Skip to content

Commit b1e7d0b

Browse files
committed
update
1 parent 85ae972 commit b1e7d0b

35 files changed

+438
-387
lines changed

lib/modules/datafile-manager/httpPollingDatafileManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export default abstract class HttpPollingDatafileManager implements DatafileMana
115115
urlTemplate = DEFAULT_URL_TEMPLATE,
116116
cache = noOpKeyValueCache,
117117
} = configWithDefaultsApplied;
118-
118+
console.log('cache is ', cache, cache.get.toString());
119119
this.cache = cache;
120120
this.cacheKey = 'opt-datafile-' + sdkKey;
121121
this.sdkKey = sdkKey;
@@ -339,6 +339,7 @@ export default abstract class HttpPollingDatafileManager implements DatafileMana
339339

340340
setDatafileFromCacheIfAvailable(): void {
341341
this.cache.get(this.cacheKey).then(datafile => {
342+
console.log('datafile is ..', datafile);
342343
if (this.isStarted && !this.isReadyPromiseSettled && datafile) {
343344
logger.debug('Using datafile from cache');
344345
this.currentDatafile = datafile;

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"clean": "rm -rf dist",
7474
"clean:win": "(if exist dist rd /s/q dist)",
7575
"lint": "tsc --noEmit && eslint 'lib/**/*.js' 'lib/**/*.ts'",
76-
"testvite": "vitest",
76+
"testvite": "tsc --noEmit --p tsconfig.spec.json && vitest",
7777
"test": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register -r lib/tests/exit_on_unhandled_rejection.js 'lib/**/*.tests.ts' 'lib/**/*.tests.js' && jest",
7878
"posttest": "npm run lint",
7979
"test-ci": "npm run test-xbrowser && npm run test-umdbrowser",
@@ -129,6 +129,7 @@
129129
"eslint": "^8.21.0",
130130
"eslint-config-prettier": "^6.10.0",
131131
"eslint-plugin-prettier": "^3.1.2",
132+
"happy-dom": "^14.12.3",
132133
"jest-environment-jsdom": "^29.0.0",
133134
"jest-localstorage-mock": "^2.4.22",
134135
"json-loader": "^0.5.4",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"typeAcquisition": {
33
"include": [
4-
"jest"
4+
"vitest"
55
]
66
}
77
}

tests/httpPollingDatafileManager.spec.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { describe, beforeEach, afterEach, it, expect, vi } from 'vitest';
16+
import { describe, beforeEach, afterEach, beforeAll, it, expect, vi, MockInstance } from 'vitest';
1717

1818
import HttpPollingDatafileManager from '../lib/modules/datafile-manager/httpPollingDatafileManager';
1919
import { Headers, AbortableRequest, Response } from '../lib/modules/datafile-manager/http';
@@ -23,16 +23,17 @@ import PersistentKeyValueCache from '../lib/plugins/key_value_cache/persistentKe
2323

2424

2525
vi.mock('../lib/modules/datafile-manager/backoffController', () => {
26-
return vi.fn().mockImplementation(() => {
27-
const getDelayMock = vi.fn().mockImplementation(() => 0);
28-
return {
29-
getDelay: getDelayMock,
30-
countError: vi.fn(),
31-
reset: vi.fn(),
32-
};
33-
});
26+
const MockBackoffController = vi.fn();
27+
MockBackoffController.prototype.getDelay = vi.fn().mockImplementation(() => 0);
28+
MockBackoffController.prototype.countError = vi.fn();
29+
MockBackoffController.prototype.reset = vi.fn();
30+
31+
return {
32+
'default': MockBackoffController,
33+
}
3434
});
3535

36+
3637
import BackoffController from '../lib/modules/datafile-manager/backoffController';
3738
import { LoggerFacade, getLogger } from '../lib/modules/logging';
3839
import { resetCalls, spy, verify } from 'ts-mockito';
@@ -481,7 +482,7 @@ describe('httpPollingDatafileManager', () => {
481482

482483
describe('backoff', () => {
483484
it('uses the delay from the backoff controller getDelay method when greater than updateInterval', async () => {
484-
const BackoffControllerMock = (BackoffController as unknown) as vi.Mock<BackoffController, []>;
485+
const BackoffControllerMock = (BackoffController as unknown) as MockInstance<() => BackoffController>;
485486
const getDelayMock = BackoffControllerMock.mock.results[0].value.getDelay;
486487
getDelayMock.mockImplementationOnce(() => 5432);
487488

@@ -513,7 +514,7 @@ describe('httpPollingDatafileManager', () => {
513514
});
514515
manager.start();
515516
await manager.responsePromises[0];
516-
const BackoffControllerMock = (BackoffController as unknown) as vi.Mock<BackoffController, []>;
517+
const BackoffControllerMock = (BackoffController as unknown) as MockInstance<() => BackoffController>;
517518
expect(BackoffControllerMock.mock.results[0].value.countError).toBeCalledTimes(1);
518519
});
519520

@@ -525,7 +526,7 @@ describe('httpPollingDatafileManager', () => {
525526
} catch (e) {
526527
//empty
527528
}
528-
const BackoffControllerMock = (BackoffController as unknown) as vi.Mock<BackoffController, []>;
529+
const BackoffControllerMock = (BackoffController as unknown) as MockInstance<() => BackoffController>;
529530
expect(BackoffControllerMock.mock.results[0].value.countError).toBeCalledTimes(1);
530531
});
531532

@@ -538,15 +539,15 @@ describe('httpPollingDatafileManager', () => {
538539
},
539540
});
540541
manager.start();
541-
const BackoffControllerMock = (BackoffController as unknown) as vi.Mock<BackoffController, []>;
542+
const BackoffControllerMock = (BackoffController as unknown) as MockInstance<() => BackoffController>;
542543
// Reset is called in start - we want to check that it is also called after the response, so reset the mock here
543544
BackoffControllerMock.mock.results[0].value.reset.mockReset();
544545
await manager.onReady();
545546
expect(BackoffControllerMock.mock.results[0].value.reset).toBeCalledTimes(1);
546547
});
547548

548549
it('resets the backoff controller when start is called', async () => {
549-
const BackoffControllerMock = (BackoffController as unknown) as vi.Mock<BackoffController, []>;
550+
const BackoffControllerMock = (BackoffController as unknown) as MockInstance<() => BackoffController>;
550551
manager.start();
551552
expect(BackoffControllerMock.mock.results[0].value.reset).toBeCalledTimes(1);
552553
try {

tests/httpPollingDatafileManagerPolling.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import { describe, beforeEach, afterEach, beforeAll, it, expect, vi, MockInstance } from 'vitest';
1617

1718
import { resetCalls, spy, verify } from 'ts-mockito';
1819
import { LogLevel, LoggerFacade, getLogger, setLogLevel } from '../lib/modules/logging';

tests/index.react_native.spec.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
/// <reference types="jest" />
16+
import { describe, beforeEach, afterEach, it, expect, vi } from 'vitest';
17+
1718
import * as logging from '../lib/modules/logging/logger';
1819
import * as eventProcessor from '../lib//plugins/event_processor/index.react_native';
1920

@@ -24,17 +25,18 @@ import optimizelyFactory from '../lib/index.react_native';
2425
import configValidator from '../lib/utils/config_validator';
2526
import eventProcessorConfigValidator from '../lib/utils/event_processor_config_validator';
2627

27-
jest.mock('react-native-get-random-values')
28-
jest.mock('fast-text-encoding')
28+
vi.mock('@react-native-community/netinfo');
29+
vi.mock('react-native-get-random-values')
30+
vi.mock('fast-text-encoding')
2931

3032
describe('javascript-sdk/react-native', () => {
3133
beforeEach(() => {
32-
jest.spyOn(optimizelyFactory.eventDispatcher, 'dispatchEvent');
33-
jest.useFakeTimers();
34+
vi.spyOn(optimizelyFactory.eventDispatcher, 'dispatchEvent');
35+
vi.useFakeTimers();
3436
});
3537

3638
afterEach(() => {
37-
jest.resetAllMocks();
39+
vi.resetAllMocks();
3840
});
3941

4042
describe('APIs', () => {
@@ -56,14 +58,14 @@ describe('javascript-sdk/react-native', () => {
5658
beforeEach(() => {
5759
// @ts-ignore
5860
silentLogger = optimizelyFactory.logging.createLogger();
59-
jest.spyOn(console, 'error');
60-
jest.spyOn(configValidator, 'validate').mockImplementation(() => {
61+
vi.spyOn(console, 'error');
62+
vi.spyOn(configValidator, 'validate').mockImplementation(() => {
6163
throw new Error('Invalid config or something');
6264
});
6365
});
6466

6567
afterEach(() => {
66-
jest.resetAllMocks();
68+
vi.resetAllMocks();
6769
});
6870

6971
it('should not throw if the provided config is not valid', () => {
@@ -131,11 +133,11 @@ describe('javascript-sdk/react-native', () => {
131133

132134
describe('when passing in logLevel', () => {
133135
beforeEach(() => {
134-
jest.spyOn(logging, 'setLogLevel');
136+
vi.spyOn(logging, 'setLogLevel');
135137
});
136138

137139
afterEach(() => {
138-
jest.resetAllMocks();
140+
vi.resetAllMocks();
139141
});
140142

141143
it('should call logging.setLogLevel', () => {
@@ -150,11 +152,11 @@ describe('javascript-sdk/react-native', () => {
150152

151153
describe('when passing in logger', () => {
152154
beforeEach(() => {
153-
jest.spyOn(logging, 'setLogHandler');
155+
vi.spyOn(logging, 'setLogHandler');
154156
});
155157

156158
afterEach(() => {
157-
jest.resetAllMocks();
159+
vi.resetAllMocks();
158160
});
159161

160162
it('should call logging.setLogHandler with the supplied logger', () => {
@@ -173,11 +175,11 @@ describe('javascript-sdk/react-native', () => {
173175
// @ts-ignore
174176
let eventProcessorSpy;
175177
beforeEach(() => {
176-
eventProcessorSpy = jest.spyOn(eventProcessor, 'createEventProcessor');
178+
eventProcessorSpy = vi.spyOn(eventProcessor, 'createEventProcessor');
177179
});
178180

179181
afterEach(() => {
180-
jest.resetAllMocks();
182+
vi.resetAllMocks();
181183
});
182184

183185
it('should use default event flush interval when none is provided', () => {
@@ -201,11 +203,11 @@ describe('javascript-sdk/react-native', () => {
201203

202204
describe('with an invalid flush interval', () => {
203205
beforeEach(() => {
204-
jest.spyOn(eventProcessorConfigValidator, 'validateEventFlushInterval').mockImplementation(() => false);
206+
vi.spyOn(eventProcessorConfigValidator, 'validateEventFlushInterval').mockImplementation(() => false);
205207
});
206208

207209
afterEach(() => {
208-
jest.resetAllMocks();
210+
vi.resetAllMocks();
209211
});
210212

211213
it('should ignore the event flush interval and use the default instead', () => {
@@ -231,11 +233,11 @@ describe('javascript-sdk/react-native', () => {
231233

232234
describe('with a valid flush interval', () => {
233235
beforeEach(() => {
234-
jest.spyOn(eventProcessorConfigValidator, 'validateEventFlushInterval').mockImplementation(() => true);
236+
vi.spyOn(eventProcessorConfigValidator, 'validateEventFlushInterval').mockImplementation(() => true);
235237
});
236238

237239
afterEach(() => {
238-
jest.resetAllMocks();
240+
vi.resetAllMocks();
239241
});
240242

241243
it('should use the provided event flush interval', () => {
@@ -278,11 +280,11 @@ describe('javascript-sdk/react-native', () => {
278280

279281
describe('with an invalid event batch size', () => {
280282
beforeEach(() => {
281-
jest.spyOn(eventProcessorConfigValidator, 'validateEventBatchSize').mockImplementation(() => false);
283+
vi.spyOn(eventProcessorConfigValidator, 'validateEventBatchSize').mockImplementation(() => false);
282284
});
283285

284286
afterEach(() => {
285-
jest.resetAllMocks();
287+
vi.resetAllMocks();
286288
});
287289

288290
it('should ignore the event batch size and use the default instead', () => {
@@ -308,11 +310,11 @@ describe('javascript-sdk/react-native', () => {
308310

309311
describe('with a valid event batch size', () => {
310312
beforeEach(() => {
311-
jest.spyOn(eventProcessorConfigValidator, 'validateEventBatchSize').mockImplementation(() => true);
313+
vi.spyOn(eventProcessorConfigValidator, 'validateEventBatchSize').mockImplementation(() => true);
312314
});
313315

314316
afterEach(() => {
315-
jest.resetAllMocks();
317+
vi.resetAllMocks();
316318
});
317319

318320
it('should use the provided event batch size', () => {

0 commit comments

Comments
 (0)