0.5.0 • Published 9 months ago

@telefonica/living-apps-core-jest v0.5.0

Weekly downloads
-
License
-
Repository
github
Last release
9 months ago

@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:

typevalueexample
functionjest.fn()laSdk.http.get
booleanfalselaSdk.video.isPlaying
numberNaNlaSdk.video.duration
string''laSdk.video.src
NullablenulllaSdk.video.videoSessionId

There are a few exceptions:

  1. laSdk.KEYS are the actual keys, we decided there was no drawback in populating KEYS with the real values. Even in this mocking library.
  2. 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
  },
}>
0.5.0

9 months ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.0

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago