Skip to content

Commit

Permalink
[FSSDK-10950] cleanup of test files (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
raju-opti authored Dec 24, 2024
1 parent e0aabf5 commit 787fbc0
Show file tree
Hide file tree
Showing 37 changed files with 226 additions and 642 deletions.
2 changes: 1 addition & 1 deletion lib/event_processor/batch_event_processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { createImpressionEvent } from '../tests/mock/create_event';
import { ProcessableEvent } from './event_processor';
import { buildLogEvent } from './event_builder/log_event';
import { resolvablePromise } from '../utils/promise/resolvablePromise';
import { advanceTimersByTime } from '../../tests/testUtils';
import { advanceTimersByTime } from '../tests/testUtils';
import { getMockLogger } from '../tests/mock/mock_logger';
import { getMockRepeater } from '../tests/mock/mock_repeater';
import * as retry from '../utils/executor/backoff_retry_runner';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ vi.mock('./default_dispatcher', () => {
return { DefaultEventDispatcher };
});

vi.mock('../../utils/http_request_handler/browser_request_handler', () => {
vi.mock('../../utils/http_request_handler/request_handler.browser', () => {
const BrowserRequestHandler = vi.fn();
return { BrowserRequestHandler };
});

import { DefaultEventDispatcher } from './default_dispatcher';
import { BrowserRequestHandler } from '../../utils/http_request_handler/browser_request_handler';
import { BrowserRequestHandler } from '../../utils/http_request_handler/request_handler.browser';
import eventDispatcher from './default_dispatcher.browser';

describe('eventDispatcher', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { BrowserRequestHandler } from "../../utils/http_request_handler/browser_request_handler";
import { BrowserRequestHandler } from "../../utils/http_request_handler/request_handler.browser";
import { EventDispatcher } from './event_dispatcher';
import { DefaultEventDispatcher } from './default_dispatcher';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ vi.mock('./default_dispatcher', () => {
return { DefaultEventDispatcher };
});

vi.mock('../../utils/http_request_handler/node_request_handler', () => {
vi.mock('../../utils/http_request_handler/request_handler.node', () => {
const NodeRequestHandler = vi.fn();
return { NodeRequestHandler };
});

import { DefaultEventDispatcher } from './default_dispatcher';
import { NodeRequestHandler } from '../../utils/http_request_handler/node_request_handler';
import { NodeRequestHandler } from '../../utils/http_request_handler/request_handler.node';
import eventDispatcher from './default_dispatcher.node';

describe('eventDispatcher', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import { EventDispatcher } from './event_dispatcher';
import { NodeRequestHandler } from '../../utils/http_request_handler/node_request_handler';
import { NodeRequestHandler } from '../../utils/http_request_handler/request_handler.node';
import { DefaultEventDispatcher } from './default_dispatcher';

const eventDispatcher: EventDispatcher = new DefaultEventDispatcher(new NodeRequestHandler());
Expand Down
4 changes: 2 additions & 2 deletions lib/index.lite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import * as enums from './utils/enums';
import * as loggerPlugin from './plugins/logger';
import Optimizely from './optimizely';
import { createNotificationCenter } from './notification_center';
import { OptimizelyDecideOption, Client, ConfigLite } from './shared_types';
import { OptimizelyDecideOption, Client, Config } from './shared_types';
import * as commonExports from './common_exports';

const logger = getLogger();
Expand All @@ -40,7 +40,7 @@ setLogLevel(LogLevel.ERROR);
* @return {Client|null} the Optimizely client object
* null on error
*/
const createInstance = function(config: ConfigLite): Client | null {
const createInstance = function(config: Config): Client | null {
try {

// TODO warn about setting per instance errorHandler / logger / logLevel
Expand Down
1 change: 0 additions & 1 deletion lib/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const createInstance = function(config: Config): Client | null {
}
}


const errorHandler = getErrorHandler();
const notificationCenter = createNotificationCenter({ logger: logger, errorHandler: errorHandler });

Expand Down
177 changes: 177 additions & 0 deletions lib/index.react_native.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/**
* Copyright 2019-2020, 2022-2024 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, beforeEach, afterEach, it, expect, vi } from 'vitest';

import * as logging from './modules/logging/logger';

import Optimizely from './optimizely';
import testData from './tests/test_data';
import packageJSON from '../package.json';
import optimizelyFactory from './index.react_native';
import configValidator from './utils/config_validator';
import { getMockProjectConfigManager } from './tests/mock/mock_project_config_manager';
import { createProjectConfig } from './project_config/project_config';

vi.mock('@react-native-community/netinfo');
vi.mock('react-native-get-random-values')
vi.mock('fast-text-encoding')

describe('javascript-sdk/react-native', () => {
beforeEach(() => {
vi.spyOn(optimizelyFactory.eventDispatcher, 'dispatchEvent');
vi.useFakeTimers();
});

afterEach(() => {
vi.resetAllMocks();
});

describe('APIs', () => {
it('should expose logger, errorHandler, eventDispatcher and enums', () => {
expect(optimizelyFactory.logging).toBeDefined();
expect(optimizelyFactory.logging.createLogger).toBeDefined();
expect(optimizelyFactory.logging.createNoOpLogger).toBeDefined();
expect(optimizelyFactory.errorHandler).toBeDefined();
expect(optimizelyFactory.eventDispatcher).toBeDefined();
expect(optimizelyFactory.enums).toBeDefined();
});

describe('createInstance', () => {
const fakeErrorHandler = { handleError: function() {} };
const fakeEventDispatcher = { dispatchEvent: async function() {
return Promise.resolve({});
} };
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
let silentLogger;

beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
silentLogger = optimizelyFactory.logging.createLogger();
vi.spyOn(console, 'error');
vi.spyOn(configValidator, 'validate').mockImplementation(() => {
throw new Error('Invalid config or something');
});
});

afterEach(() => {
vi.resetAllMocks();
});

it('should not throw if the provided config is not valid', () => {
expect(function() {
const optlyInstance = optimizelyFactory.createInstance({
projectConfigManager: getMockProjectConfigManager(),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
logger: silentLogger,
});
}).not.toThrow();
});

it('should create an instance of optimizely', () => {
const optlyInstance = optimizelyFactory.createInstance({
projectConfigManager: getMockProjectConfigManager(),
errorHandler: fakeErrorHandler,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
logger: silentLogger,
});

expect(optlyInstance).toBeInstanceOf(Optimizely);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(optlyInstance.clientVersion).toEqual('5.3.4');
});

it('should set the React Native JS client engine and javascript SDK version', () => {
const optlyInstance = optimizelyFactory.createInstance({
projectConfigManager: getMockProjectConfigManager(),
errorHandler: fakeErrorHandler,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
logger: silentLogger,
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect('react-native-js-sdk').toEqual(optlyInstance.clientEngine);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(packageJSON.version).toEqual(optlyInstance.clientVersion);
});

it('should allow passing of "react-sdk" as the clientEngine and convert it to "react-native-sdk"', () => {
const optlyInstance = optimizelyFactory.createInstance({
clientEngine: 'react-sdk',
projectConfigManager: getMockProjectConfigManager(),
errorHandler: fakeErrorHandler,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
logger: silentLogger,
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect('react-native-sdk').toEqual(optlyInstance.clientEngine);
});

describe('when passing in logLevel', () => {
beforeEach(() => {
vi.spyOn(logging, 'setLogLevel');
});

afterEach(() => {
vi.resetAllMocks();
});

it('should call logging.setLogLevel', () => {
optimizelyFactory.createInstance({
projectConfigManager: getMockProjectConfigManager({
initConfig: createProjectConfig(testData.getTestProjectConfig()),
}),
logLevel: optimizelyFactory.enums.LOG_LEVEL.ERROR,
});
expect(logging.setLogLevel).toBeCalledTimes(1);
expect(logging.setLogLevel).toBeCalledWith(optimizelyFactory.enums.LOG_LEVEL.ERROR);
});
});

describe('when passing in logger', () => {
beforeEach(() => {
vi.spyOn(logging, 'setLogHandler');
});

afterEach(() => {
vi.resetAllMocks();
});

it('should call logging.setLogHandler with the supplied logger', () => {
const fakeLogger = { log: function() {} };
optimizelyFactory.createInstance({
projectConfigManager: getMockProjectConfigManager({
initConfig: createProjectConfig(testData.getTestProjectConfig()),
}),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
logger: fakeLogger,
});
expect(logging.setLogHandler).toBeCalledTimes(1);
expect(logging.setLogHandler).toBeCalledWith(fakeLogger);
});
});
});
});
});
9 changes: 5 additions & 4 deletions tests/logger.spec.ts → lib/modules/logging/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
LogLevel,
LogHandler,
LoggerFacade,
} from '../lib/modules/logging/models'
} from './models'

import {
setLogHandler,
Expand All @@ -13,10 +13,10 @@ import {
ConsoleLogHandler,
resetLogger,
getLogLevel,
} from '../lib/modules/logging/logger'
} from './logger'

import { resetErrorHandler } from '../lib/modules/logging/errorHandler'
import { ErrorHandler, setErrorHandler } from '../lib/modules/logging/errorHandler'
import { resetErrorHandler } from './errorHandler'
import { ErrorHandler, setErrorHandler } from './errorHandler'

describe('logger', () => {
afterEach(() => {
Expand Down Expand Up @@ -302,6 +302,7 @@ describe('logger', () => {

it('should set logLevel to ERROR when setLogLevel is called with no value', () => {
const logger = new ConsoleLogHandler()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
logger.setLogLevel()

Expand Down
2 changes: 1 addition & 1 deletion lib/odp/event_manager/odp_event_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { exhaustMicrotasks } from '../../tests/testUtils';
import { OdpEvent } from './odp_event';
import { OdpConfig } from '../odp_config';
import { EventDispatchResponse } from './odp_event_api_manager';
import { advanceTimersByTime } from '../../../tests/testUtils';
import { advanceTimersByTime } from '../../tests/testUtils';

const API_KEY = 'test-api-key';
const API_HOST = 'https://odp.example.com';
Expand Down
4 changes: 2 additions & 2 deletions lib/odp/odp_manager_factory.browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

vi.mock('../utils/http_request_handler/browser_request_handler', () => {
vi.mock('../utils/http_request_handler/request_handler.browser', () => {
return { BrowserRequestHandler: vi.fn() };
});

Expand All @@ -26,7 +26,7 @@ vi.mock('./odp_manager_factory', () => {
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { getOdpManager, OdpManagerOptions } from './odp_manager_factory';
import { BROWSER_DEFAULT_API_TIMEOUT, createOdpManager } from './odp_manager_factory.browser';
import { BrowserRequestHandler } from '../utils/http_request_handler/browser_request_handler';
import { BrowserRequestHandler } from '../utils/http_request_handler/request_handler.browser';
import { pixelApiRequestGenerator } from './event_manager/odp_event_api_manager';

describe('createOdpManager', () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/odp/odp_manager_factory.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { BrowserRequestHandler } from '../utils/http_request_handler/browser_request_handler';
import { BrowserRequestHandler } from '../utils/http_request_handler/request_handler.browser';
import { pixelApiRequestGenerator } from './event_manager/odp_event_api_manager';
import { OdpManager } from './odp_manager';
import { getOdpManager, OdpManagerOptions } from './odp_manager_factory';
Expand Down
4 changes: 2 additions & 2 deletions lib/odp/odp_manager_factory.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

vi.mock('../utils/http_request_handler/node_request_handler', () => {
vi.mock('../utils/http_request_handler/request_handler.node', () => {
return { NodeRequestHandler: vi.fn() };
});

Expand All @@ -26,7 +26,7 @@ vi.mock('./odp_manager_factory', () => {
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { getOdpManager, OdpManagerOptions } from './odp_manager_factory';
import { NODE_DEFAULT_API_TIMEOUT, NODE_DEFAULT_BATCH_SIZE, NODE_DEFAULT_FLUSH_INTERVAL, createOdpManager } from './odp_manager_factory.node';
import { NodeRequestHandler } from '../utils/http_request_handler/node_request_handler';
import { NodeRequestHandler } from '../utils/http_request_handler/request_handler.node';
import { eventApiRequestGenerator } from './event_manager/odp_event_api_manager';

describe('createOdpManager', () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/odp/odp_manager_factory.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { NodeRequestHandler } from '../utils/http_request_handler/node_request_handler';
import { NodeRequestHandler } from '../utils/http_request_handler/request_handler.node';
import { eventApiRequestGenerator } from './event_manager/odp_event_api_manager';
import { OdpManager } from './odp_manager';
import { getOdpManager, OdpManagerOptions } from './odp_manager_factory';
Expand Down
4 changes: 2 additions & 2 deletions lib/odp/odp_manager_factory.react_native.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

vi.mock('../utils/http_request_handler/browser_request_handler', () => {
vi.mock('../utils/http_request_handler/request_handler.browser', () => {
return { BrowserRequestHandler: vi.fn() };
});

Expand All @@ -26,7 +26,7 @@ vi.mock('./odp_manager_factory', () => {
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { getOdpManager, OdpManagerOptions } from './odp_manager_factory';
import { RN_DEFAULT_API_TIMEOUT, RN_DEFAULT_BATCH_SIZE, RN_DEFAULT_FLUSH_INTERVAL, createOdpManager } from './odp_manager_factory.react_native';
import { BrowserRequestHandler } from '../utils/http_request_handler/browser_request_handler'
import { BrowserRequestHandler } from '../utils/http_request_handler/request_handler.browser'
import { eventApiRequestGenerator } from './event_manager/odp_event_api_manager';

describe('createOdpManager', () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/odp/odp_manager_factory.react_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { BrowserRequestHandler } from '../utils/http_request_handler/browser_request_handler';
import { BrowserRequestHandler } from '../utils/http_request_handler/request_handler.browser';
import { eventApiRequestGenerator } from './event_manager/odp_event_api_manager';
import { OdpManager } from './odp_manager';
import { getOdpManager, OdpManagerOptions } from './odp_manager_factory';
Expand Down
Loading

0 comments on commit 787fbc0

Please sign in to comment.