Skip to content

Commit c4c98a1

Browse files
[FSSDK-10882] improvements
1 parent e1bdc8b commit c4c98a1

File tree

5 files changed

+101
-24
lines changed

5 files changed

+101
-24
lines changed

lib/index.node.tests.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,7 @@ describe('optimizelyFactory', function() {
8888
assert.instanceOf(optlyInstance, Optimizely);
8989
assert.equal(optlyInstance.clientVersion, '5.3.4');
9090
});
91-
92-
it('should create an instance of optimizely with ssr flag, project config must be available', () => {
93-
const optlyInstance = optimizelyFactory.createInstance({
94-
projectConfigManager: getMockProjectConfigManager({
95-
initConfig: createProjectConfig(testData.getTestProjectConfig()),
96-
}),
97-
errorHandler: fakeErrorHandler,
98-
eventDispatcher: fakeEventDispatcher,
99-
logger: fakeLogger,
100-
isSsr: true,
101-
});
102-
103-
assert.instanceOf(optlyInstance, Optimizely);
104-
assert.equal(optlyInstance.projectConfigManager.isSsr, true);
105-
assert.deepEqual(optlyInstance.getProjectConfig(), createProjectConfig(testData.getTestProjectConfig()));
106-
});
107-
91+
10892
// TODO: user will create and inject an event processor
10993
// these tests will be refactored accordingly
11094
// describe('event processor configuration', function() {

lib/optimizely/index.spec.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright 2024, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { describe, it, expect, vi } from 'vitest';
18+
import Optimizely from '.';
19+
import { getMockProjectConfigManager } from '../tests/mock/mock_project_config_manager';
20+
import * as logger from '../plugins/logger';
21+
import * as jsonSchemaValidator from '../utils/json_schema_validator';
22+
import { LOG_LEVEL } from '../common_exports';
23+
import { createNotificationCenter } from '../core/notification_center';
24+
import testData from '../tests/test_data';
25+
import { getForwardingEventProcessor } from '../event_processor/forwarding_event_processor';
26+
import { LoggerFacade } from '../modules/logging';
27+
import { createProjectConfig } from '../project_config/project_config';
28+
29+
describe('lib/optimizely', () => {
30+
const errorHandler = { handleError: function() {} };
31+
32+
const eventDispatcher = {
33+
dispatchEvent: () => Promise.resolve({ statusCode: 200 }),
34+
};
35+
36+
const eventProcessor = getForwardingEventProcessor(eventDispatcher);
37+
38+
const createdLogger: LoggerFacade = {
39+
...logger.createLogger({
40+
logLevel: LOG_LEVEL.INFO,
41+
}),
42+
info: () => {},
43+
debug: () => {},
44+
warn: () => {},
45+
error: () => {},
46+
log: () => {},
47+
};
48+
49+
const notificationCenter = createNotificationCenter({ logger: createdLogger, errorHandler });
50+
51+
it('should pass ssr to the project config manager', () => {
52+
const projectConfigManager = getMockProjectConfigManager({
53+
initConfig: createProjectConfig(testData.getTestProjectConfig()),
54+
});
55+
56+
vi.spyOn(projectConfigManager, 'setSsr');
57+
58+
const instance = new Optimizely({
59+
clientEngine: 'node-sdk',
60+
projectConfigManager,
61+
errorHandler,
62+
jsonSchemaValidator,
63+
logger: createdLogger,
64+
notificationCenter,
65+
eventProcessor,
66+
isSsr: true,
67+
isValidInstance: true,
68+
});
69+
70+
expect(projectConfigManager.setSsr).toHaveBeenCalledWith(true);
71+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
72+
// @ts-ignore
73+
expect(instance.getProjectConfig()).toBe(projectConfigManager.config);
74+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
75+
// @ts-ignore
76+
expect(projectConfigManager.isSsr).toBe(true);
77+
});
78+
});

lib/project_config/project_config_manager.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,16 @@ describe('ProjectConfigManagerImpl', () => {
409409
expect(logger.error).toHaveBeenCalled();
410410
});
411411

412+
it('should reject onRunning() and log error if isSsr is true and datafile is not provided', async () =>{
413+
const logger = getMockLogger();
414+
const manager = new ProjectConfigManagerImpl({ logger, datafileManager: getMockDatafileManager({})});
415+
manager.setSsr(true);
416+
manager.start();
417+
418+
await expect(manager.onRunning()).rejects.toThrow();
419+
expect(logger.error).toHaveBeenCalled();
420+
});
421+
412422
it('should reject onRunning() and log error if the datafile version is not supported', async () => {
413423
const logger = getMockLogger();
414424
const datafile = testData.getUnsupportedVersionConfig();

lib/project_config/project_config_manager.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
5555
public datafileManager?: DatafileManager;
5656
private eventEmitter: EventEmitter<{ update: ProjectConfig }> = new EventEmitter();
5757
private logger?: LoggerFacade;
58-
private isSsr?: boolean;
58+
private isSsr = true;
5959

6060
constructor(config: ProjectConfigManagerConfig) {
6161
super();
@@ -75,17 +75,21 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
7575
}
7676

7777
this.state = ServiceState.Starting;
78-
if (!this.datafile && !this.datafileManager) {
79-
// TODO: replace message with imported constants
80-
this.handleInitError(new Error('You must provide at least one of sdkKey or datafile'));
81-
return;
82-
}
8378

8479
if(this.isSsr) {
8580
// If isSsr is true, we don't need to poll for datafile updates
8681
this.datafileManager = undefined
8782
}
8883

84+
if (!this.datafile && !this.datafileManager) {
85+
const errorMessage = this.isSsr
86+
? 'You must provide datafile in SSR'
87+
: 'You must provide at least one of sdkKey or datafile';
88+
// TODO: replace message with imported constants
89+
this.handleInitError(new Error(errorMessage));
90+
return;
91+
}
92+
8993
if (this.datafile) {
9094
this.handleNewDatafile(this.datafile, true);
9195
}
@@ -229,7 +233,7 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
229233
* @param {Boolean} isSsr
230234
* @returns {void}
231235
*/
232-
setSsr(isSsr?: boolean): void {
236+
setSsr(isSsr: boolean): void {
233237
this.isSsr = isSsr;
234238
}
235239
}

lib/tests/mock/mock_project_config_manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type MockOpt = {
2626

2727
export const getMockProjectConfigManager = (opt: MockOpt = {}): ProjectConfigManager => {
2828
return {
29+
isSsr: false,
2930
config: opt.initConfig,
3031
start: () => {},
3132
setSsr: function(isSsr?:boolean) {

0 commit comments

Comments
 (0)