1
- import { getConfig , initByUser } from '../js/config/dynamicConfig' ;
2
-
1
+ import { getConfig , joinWithTokenOrUrl } from '../js/config/dynamicConfig' ;
3
2
import initializedI18next from '../js/i18nextInit' ;
4
3
import { storageClear } from '../js/plugin/storage' ;
4
+ import i18next from '../js/i18nextInit' ;
5
+
5
6
window [ 'i18next' ] = initializedI18next ;
6
7
7
8
beforeEach ( ( ) => {
@@ -56,6 +57,8 @@ global.fetch = (url: string) => {
56
57
} ) as any ;
57
58
} ;
58
59
60
+ const windowAlert = jest . spyOn ( window , 'alert' ) . mockImplementation ( ( ) => { } ) ;
61
+
59
62
describe ( 'dynamicConfig' , ( ) => {
60
63
const fakeStudyName = 'gotham-city-transit' ;
61
64
const validStudyNrelCommute = 'nrel-commute' ;
@@ -65,9 +68,9 @@ describe('dynamicConfig', () => {
65
68
it ( 'should resolve with null since no config is set yet' , async ( ) => {
66
69
await expect ( getConfig ( ) ) . resolves . toBeNull ( ) ;
67
70
} ) ;
68
- it ( 'should resolve with a valid config once initByUser is called for an nrel-commute token' , async ( ) => {
71
+ it ( 'should resolve with a valid config once joinWithTokenOrUrl is called for an nrel-commute token' , async ( ) => {
69
72
const validToken = `nrelop_${ validStudyNrelCommute } _user1` ;
70
- await initByUser ( { token : validToken } ) ;
73
+ await joinWithTokenOrUrl ( validToken ) ;
71
74
const config = await getConfig ( ) ;
72
75
expect ( config ! . server . connectUrl ) . toBe ( 'https://nrel-commute-openpath.nrel.gov/api/' ) ;
73
76
expect ( config ! . joined ) . toEqual ( {
@@ -77,9 +80,9 @@ describe('dynamicConfig', () => {
77
80
} ) ;
78
81
} ) ;
79
82
80
- it ( 'should resolve with a valid config once initByUser is called for a denver-casr token' , async ( ) => {
83
+ it ( 'should resolve with a valid config once joinWithTokenOrUrl is called for a denver-casr token' , async ( ) => {
81
84
const validToken = `nrelop_${ validStudyDenverCasr } _test_user1` ;
82
- await initByUser ( { token : validToken } ) ;
85
+ await joinWithTokenOrUrl ( validToken ) ;
83
86
const config = await getConfig ( ) ;
84
87
expect ( config ! . server . connectUrl ) . toBe ( 'https://denver-casr-openpath.nrel.gov/api/' ) ;
85
88
expect ( config ! . joined ) . toEqual ( {
@@ -90,39 +93,68 @@ describe('dynamicConfig', () => {
90
93
} ) ;
91
94
} ) ;
92
95
93
- describe ( 'initByUser ' , ( ) => {
96
+ describe ( 'joinWithTokenOrUrl ' , ( ) => {
94
97
// fake study (gotham-city-transit)
95
- it ( 'should error if the study is nonexistent' , async ( ) => {
98
+ it ( 'returns false if the study is nonexistent' , async ( ) => {
96
99
const fakeBatmanToken = `nrelop_${ fakeStudyName } _batman` ;
97
- await expect ( initByUser ( { token : fakeBatmanToken } ) ) . rejects . toThrow ( ) ;
100
+ await expect ( joinWithTokenOrUrl ( fakeBatmanToken ) ) . resolves . toBe ( false ) ;
101
+ expect ( windowAlert ) . toHaveBeenLastCalledWith (
102
+ expect . stringContaining ( i18next . t ( 'config.unable-download-config' ) ) ,
103
+ ) ;
98
104
} ) ;
99
105
100
106
// real study without subgroups (nrel-commute)
101
- it ( 'should error if the study exists but the token is invalid format' , async ( ) => {
102
- const badToken1 = validStudyNrelCommute ; // doesn't start with nrelop_
103
- await expect ( initByUser ( { token : badToken1 } ) ) . rejects . toThrow ( ) ;
104
- const badToken2 = `nrelop_${ validStudyNrelCommute } ` ; // doesn't have enough _
105
- await expect ( initByUser ( { token : badToken2 } ) ) . rejects . toThrow ( ) ;
106
- const badToken3 = `nrelop_${ validStudyNrelCommute } _` ; // doesn't have user code after last _
107
- await expect ( initByUser ( { token : badToken3 } ) ) . rejects . toThrow ( ) ;
107
+ it ( 'returns false if the study exists but the token is invalid format' , async ( ) => {
108
+ const badToken1 = `nrelop_${ validStudyNrelCommute } ` ; // doesn't have enough _
109
+ await expect ( joinWithTokenOrUrl ( badToken1 ) ) . resolves . toBe ( false ) ;
110
+ expect ( windowAlert ) . toHaveBeenLastCalledWith (
111
+ expect . stringContaining (
112
+ i18next . t ( 'config.not-enough-parts-old-style' , { token : badToken1 } ) ,
113
+ ) ,
114
+ ) ;
115
+
116
+ const badToken2 = `nrelop_${ validStudyNrelCommute } _` ; // doesn't have user code after last _
117
+ await expect ( joinWithTokenOrUrl ( badToken2 ) ) . resolves . toBe ( false ) ;
118
+ expect ( windowAlert ) . toHaveBeenLastCalledWith (
119
+ expect . stringContaining (
120
+ i18next . t ( 'config.not-enough-parts-old-style' , { token : badToken2 } ) ,
121
+ ) ,
122
+ ) ;
123
+
124
+ const badToken3 = `invalid_${ validStudyNrelCommute } _user3` ; // doesn't start with nrelop_
125
+ await expect ( joinWithTokenOrUrl ( badToken3 ) ) . resolves . toBe ( false ) ;
126
+ expect ( windowAlert ) . toHaveBeenLastCalledWith (
127
+ expect . stringContaining ( i18next . t ( 'config.no-nrelop-start' , { token : badToken3 } ) ) ,
128
+ ) ;
108
129
} ) ;
109
- it ( 'should return true after successfully storing the config for a valid token' , async ( ) => {
130
+
131
+ it ( 'returns true after successfully storing the config for a valid token' , async ( ) => {
110
132
const validToken = `nrelop_${ validStudyNrelCommute } _user2` ;
111
- await expect ( initByUser ( { token : validToken } ) ) . resolves . toBe ( true ) ;
133
+ await expect ( joinWithTokenOrUrl ( validToken ) ) . resolves . toBe ( true ) ;
112
134
} ) ;
113
135
114
136
// real study with subgroups (denver-casr)
115
- it ( 'should error if the study uses subgroups but the token has no subgroup' , async ( ) => {
137
+ it ( 'returns false if the study uses subgroups but the token has no subgroup' , async ( ) => {
116
138
const tokenWithoutSubgroup = `nrelop_${ validStudyDenverCasr } _user2` ;
117
- await expect ( initByUser ( { token : tokenWithoutSubgroup } ) ) . rejects . toThrow ( ) ;
139
+ await expect ( joinWithTokenOrUrl ( tokenWithoutSubgroup ) ) . resolves . toBe ( false ) ;
140
+ expect ( windowAlert ) . toHaveBeenLastCalledWith (
141
+ expect . stringContaining (
142
+ i18next . t ( 'config.not-enough-parts' , { token : tokenWithoutSubgroup } ) ,
143
+ ) ,
144
+ ) ;
118
145
} ) ;
119
- it ( 'should error if the study uses subgroups and the token is invalid format' , async ( ) => {
146
+ it ( 'returns false if the study uses subgroups and the token is invalid format' , async ( ) => {
120
147
const badToken1 = `nrelop_${ validStudyDenverCasr } _test_` ; // doesn't have user code after last _
121
- await expect ( initByUser ( { token : badToken1 } ) ) . rejects . toThrow ( ) ;
148
+ await expect ( joinWithTokenOrUrl ( badToken1 ) ) . resolves . toBe ( false ) ;
149
+ expect ( windowAlert ) . toHaveBeenLastCalledWith (
150
+ expect . stringContaining (
151
+ i18next . t ( 'config.not-enough-parts-old-style' , { token : badToken1 } ) ,
152
+ ) ,
153
+ ) ;
122
154
} ) ;
123
- it ( 'should return true after successfully storing the config for a valid token with subgroup' , async ( ) => {
155
+ it ( 'returns true after successfully storing the config for a valid token with subgroup' , async ( ) => {
124
156
const validToken = `nrelop_${ validStudyDenverCasr } _test_user2` ;
125
- await expect ( initByUser ( { token : validToken } ) ) . resolves . toBe ( true ) ;
157
+ await expect ( joinWithTokenOrUrl ( validToken ) ) . resolves . toBe ( true ) ;
126
158
} ) ;
127
159
} ) ;
128
160
} ) ;
0 commit comments