@@ -33,7 +33,6 @@ const PKG = require('../../package.json');
33
33
const sandbox = sinon . createSandbox ( ) ;
34
34
35
35
const fakeCreds = { } as gax . grpc . ChannelCredentials ;
36
- sandbox . stub ( gax . grpc . credentials , 'createInsecure' ) . returns ( fakeCreds ) ;
37
36
38
37
const subscriptionCached = subby . Subscription ;
39
38
@@ -49,6 +48,11 @@ function Subscription(
49
48
return new overrideFn ( pubsub , name , options ) ;
50
49
}
51
50
51
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
52
+ ( Subscription as any ) . formatName_ = ( ) : string => {
53
+ return 'formatted' ;
54
+ } ;
55
+
52
56
let promisified = false ;
53
57
const fakeUtil = Object . assign ( { } , util , {
54
58
promisifySome (
@@ -92,6 +96,10 @@ class FakeTopic {
92
96
constructor ( ...args : Array < { } > ) {
93
97
this . calledWith_ = args ;
94
98
}
99
+
100
+ static formatName_ ( ) : string {
101
+ return 'foo' ;
102
+ }
95
103
}
96
104
97
105
let extended = false ;
@@ -187,6 +195,11 @@ describe('PubSub', () => {
187
195
googleAuthOverride = null ;
188
196
pubsub = new PubSub ( OPTIONS ) ;
189
197
pubsub . projectId = PROJECT_ID ;
198
+ sandbox . stub ( gax . grpc . credentials , 'createInsecure' ) . returns ( fakeCreds ) ;
199
+ } ) ;
200
+
201
+ afterEach ( ( ) => {
202
+ sandbox . restore ( ) ;
190
203
} ) ;
191
204
192
205
describe ( 'instantiation' , ( ) => {
@@ -554,13 +567,15 @@ describe('PubSub', () => {
554
567
555
568
it ( 'should return Subscription & resp to the callback' , done => {
556
569
const subscription = { } ;
557
- pubsub . subscription = ( ) => {
570
+ sandbox . stub ( pubsub , 'subscription' ) . callsFake ( ( ) => {
558
571
return subscription as subby . Subscription ;
559
- } ;
572
+ } ) ;
560
573
561
- pubsub . request = ( config , callback : Function ) => {
562
- callback ( null , apiResponse ) ;
563
- } ;
574
+ sandbox
575
+ . stub ( pubsub , 'request' )
576
+ . callsFake ( ( config , callback : Function ) => {
577
+ callback ( null , apiResponse ) ;
578
+ } ) ;
564
579
565
580
function callback (
566
581
err ?: Error | null ,
@@ -575,6 +590,31 @@ describe('PubSub', () => {
575
590
576
591
pubsub . createSubscription ?.( TOPIC_NAME , SUB_NAME , callback ) ;
577
592
} ) ;
593
+
594
+ it ( 'should fill the subscription object name if projectId was empty' , async ( ) => {
595
+ const subscription = { } ;
596
+ pubsub . projectId = undefined ;
597
+ sandbox . stub ( pubsub , 'subscription' ) . callsFake ( ( ) => {
598
+ // Simulate the project ID not being resolved.
599
+ const sub = subscription as subby . Subscription ;
600
+ sub . name = '{{projectId}}/foo/bar' ;
601
+ return sub ;
602
+ } ) ;
603
+
604
+ sandbox
605
+ . stub ( pubsub , 'request' )
606
+ . callsFake ( ( config , callback : Function ) => {
607
+ callback ( null , apiResponse ) ;
608
+ } ) ;
609
+
610
+ const [ sub , resp ] = await pubsub . createSubscription ! (
611
+ TOPIC_NAME ,
612
+ SUB_NAME
613
+ ) ! ;
614
+ assert . strictEqual ( sub , subscription ) ;
615
+ assert . strictEqual ( sub . name . includes ( '{{' ) , false ) ;
616
+ assert . strictEqual ( resp , apiResponse ) ;
617
+ } ) ;
578
618
} ) ;
579
619
} ) ;
580
620
@@ -625,12 +665,17 @@ describe('PubSub', () => {
625
665
} ) ;
626
666
627
667
describe ( 'success' , ( ) => {
628
- const apiResponse = { } ;
668
+ const apiResponse = {
669
+ name : 'new-topic' ,
670
+ } ;
671
+ let requestStub : sinon . SinonStub < unknown [ ] , unknown > ;
629
672
630
673
beforeEach ( ( ) => {
631
- pubsub . request = ( config , callback : Function ) => {
632
- callback ( null , apiResponse ) ;
633
- } ;
674
+ requestStub = sandbox
675
+ . stub ( pubsub , 'request' )
676
+ . callsFake ( ( config , callback : Function ) => {
677
+ callback ( null , apiResponse ) ;
678
+ } ) ;
634
679
} ) ;
635
680
636
681
it ( 'should return a Topic object' , done => {
@@ -656,6 +701,33 @@ describe('PubSub', () => {
656
701
done ( ) ;
657
702
} ) ;
658
703
} ) ;
704
+
705
+ it ( 'should fill the topic object name if projectId was empty' , async ( ) => {
706
+ const topicName = 'new-topic' ;
707
+ const topicInstance = { } ;
708
+
709
+ sandbox . stub ( pubsub , 'topic' ) . callsFake ( name => {
710
+ assert . strictEqual ( name , topicName ) ;
711
+
712
+ // Simulate the project ID not being resolved.
713
+ const topic = topicInstance as Topic ;
714
+ topic . name = 'projects/{{projectId}}/topics/new-topic' ;
715
+ return topic ;
716
+ } ) ;
717
+
718
+ requestStub . restore ( ) ;
719
+ sandbox
720
+ . stub ( pubsub , 'request' )
721
+ . callsFake ( ( config , callback : Function ) => {
722
+ pubsub . projectId = 'projectId' ;
723
+ callback ( null , apiResponse ) ;
724
+ } ) ;
725
+
726
+ const [ topic , resp ] = await pubsub . createTopic ! ( topicName ) ! ;
727
+ assert . strictEqual ( topic , topicInstance ) ;
728
+ assert . strictEqual ( topic . name . includes ( '{{' ) , false ) ;
729
+ assert . strictEqual ( resp , apiResponse ) ;
730
+ } ) ;
659
731
} ) ;
660
732
} ) ;
661
733
0 commit comments