Skip to content

Commit

Permalink
test: 💍 redux-saga flow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejrybaniec committed Oct 30, 2020
1 parent 7c02c81 commit cef3371
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 27 deletions.
3 changes: 3 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import '@testing-library/jest-dom';
import fetch from 'jest-fetch-mock';

fetch.enableMocks();

jest.mock('react-i18next', () => ({
useTranslation: () => {
Expand Down
40 changes: 40 additions & 0 deletions lib/js/app/modules/app/saga.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import sagaHelper from 'redux-saga-testing';
import { getContext, put } from 'redux-saga/effects';

import { setViewMode, resetVisualization } from './actions';
import { resetQueryResults } from '../queries';
import { resetSavedQuery } from '../savedQuery';
import { createNewQuery as createNewQueryFlow } from './saga';

import { PUBSUB_CONTEXT } from '../../constants';

describe('createNewQuery()', () => {
const it = sagaHelper(createNewQueryFlow());

it('change application view to editor', (result) => {
expect(result).toEqual(put(setViewMode('editor')));
});

it('get the PubSub from context', (result) => {
expect(result).toEqual(getContext(PUBSUB_CONTEXT));

return {
publish: jest.fn(),
};
});

it('publish message to query creator', () => {});

it('reset query results', (result) => {
expect(result).toEqual(put(resetQueryResults()));
});

it('reset visualization settings', (result) => {
expect(result).toEqual(put(resetVisualization()));
});

it('reset saved query settings', (result) => {
expect(result).toEqual(put(resetSavedQuery()));
});
});
90 changes: 89 additions & 1 deletion lib/js/app/modules/queries/saga.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
/* eslint-disable @typescript-eslint/camelcase */
import sagaHelper from 'redux-saga-testing';
import fetchMock from 'jest-fetch-mock';
import { getContext, put, take, select } from 'redux-saga/effects';
import HttpStatus from 'http-status-codes';

import {
deleteQuery as deleteQueryFlow,
runQuery as runQueryFlow,
saveQuery as saveQueryFlow,
checkOrganizationLimits as checkOrganizationLimitsFlow,
} from './saga';
import {
saveQuery,
saveQuerySuccess,
runQuery,
runQuerySuccess,
runQueryError,
Expand All @@ -16,13 +21,17 @@ import {
resetQueryResults,
deleteQuerySuccess,
setQueryLimitReached,
setCacheQueryLimit,
setCacheQueryLimitExceed,
} from './actions';

import {
showConfirmation,
getViewMode,
setViewMode,
selectFirstSavedQuery,
hideQuerySettingsModal,
getQuerySettingsModalVisibility,
HIDE_CONFIRMATION,
ACCEPT_CONFIRMATION,
} from '../../modules/app';
Expand All @@ -33,7 +42,86 @@ import {
KEEN_CLIENT_CONTEXT,
} from '../../constants';

import { DeleteQueryAction, RunQueryAction } from './types';
import { DeleteQueryAction, RunQueryAction, SaveQueryAction } from './types';

fetchMock.mockResponse(() => Promise.resolve(JSON.stringify({})));

describe('checkOrganizationLimits()', () => {
describe('Scenario 1: User exceed organization limits', () => {
const it = sagaHelper(checkOrganizationLimitsFlow());

it('get the Keen API client instance from context', (result) => {
expect(result).toEqual(getContext(KEEN_CLIENT_CONTEXT));

return {
url: () => 'url',
config: { masterKey: 'masterKey' },
};
});

it('calls the API to check organization limits', () => {
const response = {
cached_queries: { limited: true, limit: 5, current_usage: 10 },
};
return response;
});

it('notfies user about exceed cache queries organization limit', (result) => {
expect(result).toEqual(put(setCacheQueryLimitExceed(true)));
});

it('setup cache queries limit', (result) => {
expect(result).toEqual(put(setCacheQueryLimit(5)));
});
});
});

describe('saveQuery()', () => {
describe('Scenario 1: User successfully saves query', () => {
const query = { analysis_type: 'count' };
const action = saveQuery('purchases', query) as SaveQueryAction;
const it = sagaHelper(saveQueryFlow(action));

it('get the notification manager from context', (result) => {
expect(result).toEqual(getContext(NOTIFICATION_MANAGER_CONTEXT));

return {
showNotification: jest.fn(),
};
});

it('get the Keen API client instance from context', (result) => {
expect(result).toEqual(getContext(KEEN_CLIENT_CONTEXT));

return {
put: jest.fn(),
url: () => 'url',
config: { masterKey: 'masterKey' },
};
});

it('get settings modal visibility from state', (result) => {
expect(result).toEqual(select(getQuerySettingsModalVisibility));
return true;
});

it('calls API to save query resource', () => {
return {
success: true,
};
});

it('hide query settings modal', (result) => {
expect(result).toEqual(put(hideQuerySettingsModal()));
});

it('dispatch save query success action', (result) => {
expect(result).toEqual(
put(saveQuerySuccess('purchases', { success: true }))
);
});
});
});

describe('runQuery()', () => {
describe('Scenario 1: User successfully run query', () => {
Expand Down
4 changes: 2 additions & 2 deletions lib/js/app/modules/queries/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function* runQuery(action: RunQueryAction) {
}
}

function* saveQuery({ payload }: SaveQueryAction) {
export function* saveQuery({ payload }: SaveQueryAction) {
try {
const { name, body } = payload;
const notificationManager = yield getContext(NOTIFICATION_MANAGER_CONTEXT);
Expand Down Expand Up @@ -287,7 +287,7 @@ function* fetchSavedQueries() {
}
}

function* checkOrganizationLimits() {
export function* checkOrganizationLimits() {
try {
const client = yield getContext(KEEN_CLIENT_CONTEXT);
const url = client.url('/3.0/projects/{projectId}/organization-usage', {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"jest": "^26.0.1",
"jest-environment-jsdom-c3": "^2.0.0",
"jest-environment-jsdom-sixteen": "^1.0.3",
"jest-fetch-mock": "^2.1.2",
"jest-fetch-mock": "^3.0.3",
"jest-styled-components": "^7.0.2",
"lint-staged": "^10.2.11",
"prettier": "^2.0.5",
Expand Down
40 changes: 17 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4370,13 +4370,12 @@ cross-env@^7.0.2:
dependencies:
cross-spawn "^7.0.1"

cross-fetch@^2.2.2:
version "2.2.3"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-2.2.3.tgz#e8a0b3c54598136e037f8650f8e823ccdfac198e"
integrity sha512-PrWWNH3yL2NYIb/7WF/5vFG3DCQiXDOVf8k3ijatbrtnwNuhMWLC7YF7uqf53tbTFDzHIUD8oITw4Bxt8ST3Nw==
cross-fetch@^3.0.4:
version "3.0.6"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.0.6.tgz#3a4040bc8941e653e0e9cf17f29ebcd177d3365c"
integrity sha512-KBPUbqgFjzWlVcURG+Svp9TlhA5uliYtiNx/0r8nv0pdypeQCRJ9IaSIc3q/x3q8t3F75cHuwxVql1HFGHCNJQ==
dependencies:
node-fetch "2.1.2"
whatwg-fetch "2.0.4"
node-fetch "2.6.1"

cross-spawn@^6.0.0, cross-spawn@^6.0.5:
version "6.0.5"
Expand Down Expand Up @@ -8100,13 +8099,13 @@ jest-environment-node@^26.5.2:
jest-mock "^26.5.2"
jest-util "^26.5.2"

jest-fetch-mock@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/jest-fetch-mock/-/jest-fetch-mock-2.1.2.tgz#1260b347918e3931c4ec743ceaf60433da661bd0"
integrity sha512-tcSR4Lh2bWLe1+0w/IwvNxeDocMI/6yIA2bijZ0fyWxC4kQ18lckQ1n7Yd40NKuisGmcGBRFPandRXrW/ti/Bw==
jest-fetch-mock@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/jest-fetch-mock/-/jest-fetch-mock-3.0.3.tgz#31749c456ae27b8919d69824f1c2bd85fe0a1f3b"
integrity sha512-Ux1nWprtLrdrH4XwE7O7InRY6psIi3GOsqNESJgMJ+M5cv4A8Lh7SN9d2V2kKRZ8ebAfcd1LNyZguAOb6JiDqw==
dependencies:
cross-fetch "^2.2.2"
promise-polyfill "^7.1.1"
cross-fetch "^3.0.4"
promise-polyfill "^8.1.3"

jest-get-type@^25.2.6:
version "25.2.6"
Expand Down Expand Up @@ -9584,11 +9583,6 @@ no-case@^3.0.3:
lower-case "^2.0.1"
tslib "^1.10.0"

node-fetch@2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.1.2.tgz#ab884e8e7e57e38a944753cec706f788d1768bb5"
integrity sha1-q4hOjn5X44qUR1POxwb3iNF2i7U=

node-fetch@2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
Expand Down Expand Up @@ -10546,16 +10540,16 @@ promise-inflight@^1.0.1:
resolved "https://registry.yarnpkg.com/promise-inflight/-/promise-inflight-1.0.1.tgz#98472870bf228132fcbdd868129bad12c3c029e3"
integrity sha1-mEcocL8igTL8vdhoEputEsPAKeM=

promise-polyfill@^7.1.1:
version "7.1.2"
resolved "https://registry.yarnpkg.com/promise-polyfill/-/promise-polyfill-7.1.2.tgz#ab05301d8c28536301622d69227632269a70ca3b"
integrity sha512-FuEc12/eKqqoRYIGBrUptCBRhobL19PS2U31vMNTfyck1FxPyMfgsXyW4Mav85y/ZN1hop3hOwRlUDok23oYfQ==

promise-polyfill@^8.0.0:
version "8.1.3"
resolved "https://registry.yarnpkg.com/promise-polyfill/-/promise-polyfill-8.1.3.tgz#8c99b3cf53f3a91c68226ffde7bde81d7f904116"
integrity sha512-MG5r82wBzh7pSKDRa9y+vllNHz3e3d4CNj1PQE4BQYxLme0gKYYBm9YENq+UkEikyZ0XbiGWxYlVw3Rl9O/U8g==

promise-polyfill@^8.1.3:
version "8.2.0"
resolved "https://registry.yarnpkg.com/promise-polyfill/-/promise-polyfill-8.2.0.tgz#367394726da7561457aba2133c9ceefbd6267da0"
integrity sha512-k/TC0mIcPVF6yHhUvwAp7cvL6I2fFV7TzF1DuGPI8mBh4QQazf36xCKEHKTZKRysEoTQoQdKyP25J8MPJp7j5g==

prompts@^2.0.1:
version "2.3.2"
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.3.2.tgz#480572d89ecf39566d2bd3fe2c9fccb7c4c0b068"
Expand Down Expand Up @@ -13591,7 +13585,7 @@ whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.3, whatwg-encoding@^1.0.5:
dependencies:
iconv-lite "0.4.24"

whatwg-fetch@2.0.4, whatwg-fetch@^2.0.4:
whatwg-fetch@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.4.tgz#dde6a5df315f9d39991aa17621853d720b85566f"
integrity sha512-dcQ1GWpOD/eEQ97k66aiEVpNnapVj90/+R+SXTPYGHpYBBypfKJEQjLrvMZ7YXbKm21gXd4NcuxUTjiv1YtLng==
Expand Down

0 comments on commit cef3371

Please sign in to comment.