@@ -11,7 +11,15 @@ describe('api', () => {
1111 const clientId = 'clientId' ;
1212 const code = 'code' ;
1313 const redirectUri = 'redirectUri' ;
14- const token = 'token' ;
14+ const token = {
15+ access_token : 'access_token' ,
16+ refresh_token : 'refresh_token' ,
17+ expires_in : 3600 ,
18+ token_type : 'bearer' ,
19+ scope : 'guest_read' ,
20+ created_at : Date . now ( ) ,
21+ resource_server : 'jp-api'
22+ } ;
1523 const state = 'state' ;
1624
1725 const mtLinkSdk = new MtLinkSdk ( ) ;
@@ -48,8 +56,7 @@ describe('api', () => {
4856
4957 test ( 'make request' , async ( ) => {
5058 fetch . mockClear ( ) ;
51-
52- fetch . mockResponseOnce ( JSON . stringify ( { access_token : token } ) ) ;
59+ fetch . mockResponseOnce ( JSON . stringify ( token ) ) ;
5360
5461 await exchangeToken ( mtLinkSdk . storedOptions , { code, codeVerifier : '' } ) ;
5562
@@ -94,36 +101,33 @@ describe('api', () => {
94101
95102 test ( 'auto extract code from url query if no code was passed' , async ( ) => {
96103 fetch . mockClear ( ) ;
104+ fetch . mockResponseOnce ( JSON . stringify ( token ) ) ;
97105
98- const code1 = 'code1' ;
99- const code2 = 'code2' ;
100-
101- fetch . mockResponseOnce ( JSON . stringify ( { access_token : token } ) ) ;
106+ const code = 'realCode' ;
102107
103108 jest . spyOn ( window , 'location' , 'get' ) . mockReturnValueOnce ( {
104- search : `?code=${ code1 } &code=${ code2 } `
109+ search : `?code=otherCode &code=${ code } `
105110 } as typeof window . location ) ;
106111
107112 await exchangeToken ( mtLinkSdk . storedOptions , { state } ) ;
108113
109114 const result = fetch . mock . calls [ 0 ] [ 1 ] || { } ;
110115 const data = JSON . parse ( result . body as string ) ;
111116
112- expect ( data . code ) . toBe ( code2 ) ;
117+ expect ( data . code ) . toBe ( code ) ;
113118 } ) ;
114119
115120 test ( 'auto extract state from url query if no state was passed or set during init' , async ( ) => {
116121 fetch . mockClear ( ) ;
117-
118- const state1 = 'state1' ;
119-
120- fetch . mockResponseOnce ( JSON . stringify ( { access_token : token } ) ) ;
122+ fetch . mockResponseOnce ( JSON . stringify ( token ) ) ;
121123
122124 jest . spyOn ( window , 'location' , 'get' ) . mockReturnValueOnce ( {
123- search : `?state=${ state1 } &state=${ state } `
125+ search : `?state=otherState &state=${ state } `
124126 } as typeof window . location ) ;
125127
126- await expect ( exchangeToken ( mtLinkSdk . storedOptions , { code, redirectUri } ) ) . resolves . toBe ( token ) ;
128+ const actual = await exchangeToken ( mtLinkSdk . storedOptions , { code, redirectUri } ) ;
129+
130+ expect ( actual ) . toEqual ( token ) ;
127131 } ) ;
128132
129133 test ( 'non browser environment will not auto extract code from url' , async ( ) => {
0 commit comments