-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add more rn sdk unit tests. (#339)
* Added more coverage for rn sdk. * Added separate tsconfig.json for tests. * Improved jest config. * Removed release-as from release please config. --------- Co-authored-by: LaunchDarklyReleaseBot <[email protected]>
- Loading branch information
Showing
12 changed files
with
179 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { JestConfigWithTsJest } from 'ts-jest'; | ||
|
||
const jestConfig: JestConfigWithTsJest = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'jsdom', | ||
transform: { | ||
'^.+\\.tsx?$': [ | ||
'ts-jest', | ||
{ | ||
tsconfig: 'tsconfig.test.json', | ||
}, | ||
], | ||
}, | ||
testPathIgnorePatterns: ['node_modules', 'example', 'dist'], | ||
}; | ||
|
||
export default jestConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
packages/sdk/react-native/src/provider/LDProvider.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { render } from '@testing-library/react'; | ||
|
||
import type { LDContext, LDOptions } from '@launchdarkly/js-client-sdk-common'; | ||
|
||
import { useLDClient } from '../hooks'; | ||
import ReactNativeLDClient from '../ReactNativeLDClient'; | ||
import LDProvider from './LDProvider'; | ||
import setupListeners from './setupListeners'; | ||
|
||
jest.mock('./setupListeners'); | ||
jest.mock('../ReactNativeLDClient'); | ||
|
||
const TestApp = () => { | ||
const ldClient = useLDClient(); | ||
return ( | ||
<> | ||
<p>ldClient {ldClient ? 'defined' : 'undefined'}</p> | ||
<p>mobileKey {ldClient.sdkKey ? ldClient.sdkKey : 'undefined'}</p> | ||
<p>context {ldClient.getContext() ? 'defined' : 'undefined'}</p> | ||
</> | ||
); | ||
}; | ||
describe('LDProvider', () => { | ||
let ldc: ReactNativeLDClient; | ||
let context: LDContext; | ||
let mockSetupListeners = setupListeners as jest.Mock; | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
(ReactNativeLDClient as jest.Mock).mockImplementation( | ||
(mobileKey: string, _options?: LDOptions) => { | ||
let context: LDContext; | ||
|
||
return { | ||
sdkKey: mobileKey, | ||
identify: jest.fn((c: LDContext) => { | ||
context = c; | ||
return Promise.resolve(); | ||
}), | ||
getContext: jest.fn(() => context), | ||
on: jest.fn(), | ||
logger: { | ||
debug: jest.fn(), | ||
}, | ||
}; | ||
}, | ||
); | ||
mockSetupListeners.mockImplementation((client: ReactNativeLDClient, setState: any) => { | ||
setState({ client }); | ||
}); | ||
ldc = new ReactNativeLDClient('mobile-key'); | ||
context = { kind: 'user', key: 'test-user-key-1' }; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('client is correctly set', () => { | ||
const { getByText } = render( | ||
<LDProvider client={ldc}> | ||
<TestApp /> | ||
</LDProvider>, | ||
); | ||
|
||
expect(getByText(/ldclient defined/i)).toBeTruthy(); | ||
expect(getByText(/mobilekey mobile-key/i)).toBeTruthy(); | ||
expect(getByText(/context undefined/i)).toBeTruthy(); | ||
}); | ||
|
||
test('specified context is identified', async () => { | ||
const { getByText } = render( | ||
<LDProvider client={ldc} context={context}> | ||
<TestApp /> | ||
</LDProvider>, | ||
); | ||
|
||
expect(mockSetupListeners).toHaveBeenCalledWith(ldc, expect.any(Function)); | ||
expect(ldc.identify).toHaveBeenCalledWith(context); | ||
expect(ldc.getContext()).toEqual(context); | ||
expect(getByText(/context defined/i)).toBeTruthy(); | ||
}); | ||
|
||
test('identify errors are caught', async () => { | ||
(ldc.identify as jest.Mock).mockImplementation(() => { | ||
return Promise.reject('faking error when identifying'); | ||
}); | ||
const { getByText } = render( | ||
<LDProvider client={ldc} context={context}> | ||
<TestApp /> | ||
</LDProvider>, | ||
); | ||
await jest.runAllTimersAsync(); | ||
|
||
expect(ldc.logger.debug).toHaveBeenCalledWith(expect.stringMatching(/identify error/)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/sdk/react-native/src/provider/setupListeners.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import ReactNativeLDClient from '../ReactNativeLDClient'; | ||
import setupListeners from './setupListeners'; | ||
|
||
import resetAllMocks = jest.resetAllMocks; | ||
|
||
jest.mock('../ReactNativeLDClient'); | ||
|
||
describe('setupListeners', () => { | ||
let ldc: ReactNativeLDClient; | ||
let mockSetState: jest.Mock; | ||
|
||
beforeEach(() => { | ||
mockSetState = jest.fn(); | ||
ldc = new ReactNativeLDClient('mob-test-key'); | ||
}); | ||
|
||
afterEach(() => resetAllMocks()); | ||
|
||
test('change listener is setup', () => { | ||
setupListeners(ldc, mockSetState); | ||
expect(ldc.on).toHaveBeenCalledWith('change', expect.any(Function)); | ||
}); | ||
|
||
test('client is set on change event', () => { | ||
setupListeners(ldc, mockSetState); | ||
|
||
const changeHandler = (ldc.on as jest.Mock).mock.calls[0][1]; | ||
changeHandler(); | ||
|
||
expect(mockSetState).toHaveBeenCalledWith({ client: ldc }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"jsx": "react-jsx", | ||
"lib": ["es6", "dom"], | ||
"module": "ES6", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"rootDir": ".", | ||
"strict": true, | ||
"types": ["jest", "node"] | ||
}, | ||
"exclude": ["dist", "node_modules", "__tests__", "example"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters