Skip to content

Commit

Permalink
Merge branch 'refactor_reducer_and_actions' into add_shared_clients_s…
Browse files Browse the repository at this point in the history
…tatus_to_state
  • Loading branch information
EmilianoSanchez committed Sep 10, 2024
2 parents e49acdd + 792927d commit 385443c
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 33 deletions.
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ module.exports = {
collectCoverageFrom: [
'src/**/*.{js,jsx,ts,tsx}',
'!src/__tests__/**',
]
],

// Custom jest matcher
setupFilesAfterEnv: ['<rootDir>/src/__tests__/utils/toBeWithinRange.ts'],
};
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@types/jest": "^27.0.0",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/redux-mock-store": "^1.0.1",
Expand Down
54 changes: 36 additions & 18 deletions src/__tests__/actions.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,25 @@ describe('initSplitSdk', () => {
actionResult.then(() => {
// return of async action
let action = store.getActions()[0];
expect(action.type).toEqual(SPLIT_READY);
expect(action.payload.timestamp).toBeLessThanOrEqual(Date.now() + 1);
expect(action.payload.timestamp).toBeGreaterThanOrEqual(timestamp);
expect(action).toEqual({
type: SPLIT_READY,
payload: {
timestamp: expect.toBeWithinRange(timestamp, Date.now() + 1),
}
});
expect((SplitFactory as jest.Mock).mock.calls.length).toBe(1);
expect(onReadyCb.mock.calls.length).toBe(1);

timestamp = Date.now();
(splitSdk.factory as any).client().__emitter__.emit(Event.SDK_UPDATE);
setTimeout(() => {
action = store.getActions()[1];
expect(action.type).toEqual(SPLIT_UPDATE);
expect(action.payload.timestamp).toBeLessThanOrEqual(Date.now() + 1);
expect(action.payload.timestamp).toBeGreaterThanOrEqual(timestamp);
expect(action).toEqual({
type: SPLIT_UPDATE,
payload: {
timestamp: expect.toBeWithinRange(timestamp, Date.now() + 1),
}
});
expect(onUpdateCb.mock.calls.length).toBe(1);
done();
}, 0);
Expand All @@ -79,19 +85,25 @@ describe('initSplitSdk', () => {
actionResult.catch(() => {
// return of async action
let action = store.getActions()[0];
expect(action.type).toEqual(SPLIT_TIMEDOUT);
expect(action.payload.timestamp).toBeLessThanOrEqual(Date.now() + 1);
expect(action.payload.timestamp).toBeGreaterThanOrEqual(timestamp);
expect(action).toEqual({
type: SPLIT_TIMEDOUT,
payload: {
timestamp: expect.toBeWithinRange(timestamp, Date.now() + 1),
}
});
expect((SplitFactory as jest.Mock).mock.calls.length).toBe(1);
expect(onTimedoutCb.mock.calls.length).toBe(1);

timestamp = Date.now();
(splitSdk.factory as any).client().__emitter__.emit(Event.SDK_READY);
setTimeout(() => {
action = store.getActions()[1];
expect(action.type).toEqual(SPLIT_READY);
expect(action.payload.timestamp).toBeLessThanOrEqual(Date.now() + 1);
expect(action.payload.timestamp).toBeGreaterThanOrEqual(timestamp);
expect(action).toEqual({
type: SPLIT_READY,
payload: {
timestamp: expect.toBeWithinRange(timestamp, Date.now() + 1),
}
});
expect(onReadyCb.mock.calls.length).toBe(1);
done();
}, 0);
Expand All @@ -105,9 +117,12 @@ describe('initSplitSdk', () => {
const onReadyFromCacheCb = jest.fn(() => {
// action should be already dispatched when the callback is called
const action = store.getActions()[0];
expect(action.type).toEqual(SPLIT_READY_FROM_CACHE);
expect(action.payload.timestamp).toBeLessThanOrEqual(Date.now() + 1);
expect(action.payload.timestamp).toBeGreaterThanOrEqual(timestamp);
expect(action).toEqual({
type: SPLIT_READY_FROM_CACHE,
payload: {
timestamp: expect.toBeWithinRange(timestamp, Date.now() + 1),
}
});
});
const onReadyCb = jest.fn(() => {
const action = store.getActions()[1];
Expand Down Expand Up @@ -570,9 +585,12 @@ describe('destroySplitSdk', () => {

actionResult.then(() => {
const action = store.getActions()[3];
expect(action.type).toEqual(SPLIT_DESTROY);
expect(action.payload.timestamp).toBeLessThanOrEqual(Date.now() + 1);
expect(action.payload.timestamp).toBeGreaterThanOrEqual(timestamp);
expect(action).toEqual({
type: SPLIT_DESTROY,
payload: {
timestamp: expect.toBeWithinRange(timestamp, Date.now() + 1),
}
});
// assert that all client's destroy methods were called
expect(splitSdk.factory.client().destroy).toBeCalledTimes(1);
expect(splitSdk.factory.client('other-user-key').destroy).toBeCalledTimes(1);
Expand Down
36 changes: 24 additions & 12 deletions src/__tests__/actions.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ describe('initSplitSdk', () => {

// Action is dispatched synchronously
const action = store.getActions()[0];
expect(action.type).toEqual(SPLIT_READY);
expect(action.payload.timestamp).toBeLessThanOrEqual(Date.now() + 1);
expect(action.payload.timestamp).toBeGreaterThanOrEqual(timestamp);
expect(action).toEqual({
type: SPLIT_READY,
payload: {
timestamp: expect.toBeWithinRange(timestamp, Date.now()),
}
});
}

// create multiple stores
Expand Down Expand Up @@ -86,9 +89,12 @@ describe('initSplitSdk', () => {
store.dispatch<any>(initSplitSdkAction);

const action = store.getActions()[0];
expect(action.type).toEqual(SPLIT_TIMEDOUT);
expect(action.payload.timestamp).toBeLessThanOrEqual(Date.now() + 1);
expect(action.payload.timestamp).toBeGreaterThanOrEqual(timestamp);
expect(action).toEqual({
type: SPLIT_TIMEDOUT,
payload: {
timestamp: expect.toBeWithinRange(timestamp, Date.now() + 1)
}
});
expect((SplitFactory as jest.Mock).mock.calls.length).toBe(1);

timestamp = Date.now();
Expand All @@ -105,14 +111,20 @@ describe('initSplitSdk', () => {

// Actions are dispatched synchronously
const timeoutAction = store.getActions()[0];
expect(timeoutAction.type).toEqual(SPLIT_TIMEDOUT);
expect(timeoutAction.payload.timestamp).toBeLessThanOrEqual(Date.now() + 1);
expect(timeoutAction.payload.timestamp).toBeGreaterThanOrEqual(timestamp);
expect(timeoutAction).toEqual({
type: SPLIT_TIMEDOUT,
payload: {
timestamp: expect.toBeWithinRange(timestamp, Date.now() + 1)
}
});

const readyAction = store.getActions()[1];
expect(readyAction.type).toEqual(SPLIT_READY);
expect(readyAction.payload.timestamp).toBeLessThanOrEqual(Date.now() + 1);
expect(readyAction.payload.timestamp).toBeGreaterThanOrEqual(timestamp);
expect(readyAction).toEqual({
type: SPLIT_READY,
payload: {
timestamp: expect.toBeWithinRange(timestamp, Date.now() + 1)
}
});
}

// create multiple stores
Expand Down
36 changes: 36 additions & 0 deletions src/__tests__/utils/toBeWithinRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable @typescript-eslint/no-namespace */
/* eslint-disable @typescript-eslint/no-empty-interface */

// Custom matcher https://jest-archive-august-2023.netlify.app/docs/27.x/expect#expectextendmatchers
import { expect } from '@jest/globals';

expect.extend({
toBeWithinRange(received: any, floor: number, ceiling: number) {
const pass = received >= floor && received <= ceiling;
if (pass) {
return {
message: () =>
`expected ${received} not to be within range ${floor} - ${ceiling}`,
pass: true,
};
} else {
return {
message: () =>
`expected ${received} to be within range ${floor} - ${ceiling}`,
pass: false,
};
}
},
});

interface CustomMatchers<R = unknown> {
toBeWithinRange(floor: number, ceiling: number): R;
}

declare global {
namespace jest {
interface Expect extends CustomMatchers { }
interface Matchers<R> extends CustomMatchers<R> { }
interface InverseAsymmetricMatchers extends CustomMatchers { }
}
}

0 comments on commit 385443c

Please sign in to comment.