Skip to content

Commit f1a68e4

Browse files
committed
save
1 parent 08636b7 commit f1a68e4

File tree

8 files changed

+166
-48
lines changed

8 files changed

+166
-48
lines changed

lib/index.browser.tests.js

Lines changed: 84 additions & 40 deletions
Large diffs are not rendered by default.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { test, vi, expect } from 'vitest';
2+
3+
import { StaticProjectConfigManager } from './static_project_config_manager';
4+
import { ServiceState } from '../service';
5+
import testData from '../tests/test_data';
6+
import { advanceTimersByTime } from '../../tests/testUtils';
7+
8+
test('state is New on instatiation', () => {
9+
const configManagaer = new StaticProjectConfigManager({});
10+
expect(configManagaer.getState()).toEqual(ServiceState.New);
11+
});
12+
13+
test('promise returned from onRunning is is not fulfilled until start() is called', async() => {
14+
const configManagaer = new StaticProjectConfigManager({});
15+
const startSpy = vi.spyOn(configManagaer, 'start');
16+
const promiseSpy = vi.fn();
17+
18+
configManagaer.onRunning().then(promiseSpy, promiseSpy);
19+
20+
vi.useFakeTimers();
21+
await advanceTimersByTime(10000);
22+
23+
expect(promiseSpy).not.toHaveBeenCalled();
24+
25+
configManagaer.start();
26+
await Promise.resolve();
27+
28+
expect(promiseSpy).toHaveBeenCalledOnce();
29+
vi.useRealTimers();
30+
});
31+
32+
test('promise returned from onRunning is rejected in case of invalid datafile', () => {
33+
const configManagaer = new StaticProjectConfigManager({});
34+
configManagaer.start();
35+
expect(configManagaer.onRunning()).rejects.toBeInstanceOf(Error);
36+
});
37+
38+
test('promise returned from onRunning is rejected in case of invalid datafile', () => {
39+
const configManagaer = new StaticProjectConfigManager({});
40+
configManagaer.start();
41+
expect(configManagaer.onRunning()).rejects.toBeInstanceOf(Error);
42+
});
43+
44+
test('promise returned from onRunning is resolved in case of valid datafile', () => {
45+
const configManagaer = new StaticProjectConfigManager(testData.getTestProjectConfig());
46+
configManagaer.start();
47+
expect(configManagaer.onRunning()).resolves.toBeUndefined();
48+
});

lib/project_config/static_project_config_manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export class StaticProjectConfigManager implements ProjectConfigManager {
2828
this.stopPrommise = resolvablePromise();
2929
}
3030

31-
getOptimizelyConfig(): OptimizelyConfig | null {
32-
return this.optimizelyConfig || null;
31+
getOptimizelyConfig(): OptimizelyConfig | undefined {
32+
return this.optimizelyConfig;
3333
}
3434

3535
getState(): ServiceState {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sinon from 'sinon';
2+
3+
export const getMockProjectConfigManager = (opt = {}) => {
4+
return {
5+
config: opt.initConfig,
6+
start: sinon.spy(),
7+
onRunning: () => opt.onRunning || Promise.resolve(),
8+
stop: sinon.spy(),
9+
onTerminated: () => opt.onTerminated || Promise.resolve(),
10+
getConfig: function() {
11+
return this.config;
12+
},
13+
setConfig: function(config) {
14+
this.config = config;
15+
},
16+
onUpdate: function(listener) {
17+
if (this.listeners === undefined) {
18+
this.listeners = [];
19+
}
20+
this.listeners.push(listener);
21+
return () => {};
22+
},
23+
pushUpdate: function(config) {
24+
this.config = config;
25+
this.listeners.forEach((listener) => listener(config));
26+
}
27+
}
28+
};

lib/utils/event_emitter/event_emitter.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { it, vi, expect } from 'vitest';
22

33
import { EventEmitter } from './event_emitter';
4-
import exp from 'constants';
5-
import { emit, removeAllListeners } from 'process';
64

75
it('should call all registered listeners correctly on emit event', () => {
86
const emitter = new EventEmitter<{ foo: number, bar: string, baz: boolean}>();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"clean:win": "(if exist dist rd /s/q dist)",
7575
"lint": "tsc --noEmit && eslint 'lib/**/*.js' 'lib/**/*.ts'",
7676
"test-vitest": "tsc --noEmit --p tsconfig.spec.json && vitest run",
77-
"test-mocha": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register -r lib/tests/exit_on_unhandled_rejection.js 'lib/optimizely/index.tests.js'",
77+
"test-mocha": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register -r lib/tests/exit_on_unhandled_rejection.js 'lib/index.node.tests.js'",
7878
"test": "npm run test-mocha && npm run test-vitest",
7979
"posttest": "npm run lint",
8080
"test-ci": "npm run test-xbrowser && npm run test-umdbrowser",

tsconfig.spec.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
44
"types": [
5-
"vitest/jsdom"
5+
"vitest/jsdom",
66
],
77
"typeRoots": [
8-
"./node_modules/@types"
8+
"./node_modules/@types", "."
99
]
1010
},
1111
"include": [

vitest.config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default defineConfig({
44
test: {
55
onConsoleLog: () => true,
66
environment: 'happy-dom',
7-
include: ['**/event_emitter.spec.ts'],
7+
include: ["**/static_project_config_manager.spec.ts"],
88
typecheck: {
99
tsconfig: 'tsconfig.spec.json',
1010
exclude: ['**/index.react_native.spec.ts'],

0 commit comments

Comments
 (0)