0.5.0 • Published 9 months ago
@telefonica/living-apps-core-jest v0.5.0
@telefonica/living-apps-core-jest
Mocks for testing
laSdk
calls with Jest
Installation
npm install --save-dev @telefonica/living-apps-core-jest
Import @telefonica/living-apps-core-jest
once (for instance in your tests setup file) and you're good to go:
// In your own jest.setup.js (or any other name)
import '@telefonica/living-apps-core-jest';
// In jest.config.js add (if you haven't already)
setupFilesAfterEnv: ['<rootDir>/jest-setup.js'];
You will also need to include your setup file in your `tsconfig.json if you haven't already:
// In tsconfig.json
"include": [
...
"./jest-setup.ts"
],
Examples
test('your test', async () => {
laSdk.telefonicaApi.get.mockResolvedValueOnce({
value: 42,
});
const result = await someFunctionThatUsesGet();
expect(result).toEqual({ value: 42 });
});
test('your test', async () => {
render(<YourComponent />);
expect(laSdk.focus).toHaveBeenCalledWith('some-element');
});
test('your test', async () => {
render(<GenericWorldController {...} />);
expect(laSdk.getMakerContents).toHaveBeenCalledTimes(1);
expect(laSdk.getMakerContents).toHaveBeenCalledWith('YOUR SCREEN NAME');
Mocked Values
All the values from laSdk
are set to the "most falsy" value possible that honor the type definitions. Except functions which are all jest.fn()
functions; meaning:
type | value | example |
---|---|---|
function | jest.fn() | laSdk.http.get |
boolean | false | laSdk.video.isPlaying |
number | NaN | laSdk.video.duration |
string | '' | laSdk.video.src |
Nullable | null | laSdk.video.videoSessionId |
There are a few exceptions:
laSdk.KEYS
are the actual keys, we decided there was no drawback in populating KEYS with the real values. Even in this mocking library.- Honoring
@telefonica/living-apps-core-web-types
there are some strings that are specifically defined, in those cases we choose one from those instead of the empty string; example:laSdk.locale #=> 'es-ES'
Caveats
- This package is executing a
beforeEach()
with the following mock setup (if this setup cause any trouble please contact with the team):
laSdk.persistenceRead() => Promise<void>
laSdk.getMakerContents() => Promise<{
status: 200,
json: {
id: '__mocked_layout_id__',
customType: 'layout_text',
question: '',
background: {},
suggestions: [], s
},
}>