From eb6fd773370d46e62d985321d775d8eb6df05bff Mon Sep 17 00:00:00 2001 From: Nelson Fernandes Date: Tue, 22 Oct 2019 09:20:55 +0100 Subject: [PATCH] test: Add repository and organization actions tests --- .../tests/actions/organization.action.js | 97 +++++ __tests__/tests/actions/repository.action.js | 375 ++++++++++++++++++ package.json | 1 + yarn.lock | 12 + 4 files changed, 485 insertions(+) create mode 100644 __tests__/tests/actions/organization.action.js create mode 100644 __tests__/tests/actions/repository.action.js diff --git a/__tests__/tests/actions/organization.action.js b/__tests__/tests/actions/organization.action.js new file mode 100644 index 000000000..8f2a27427 --- /dev/null +++ b/__tests__/tests/actions/organization.action.js @@ -0,0 +1,97 @@ +import configureStore from 'redux-mock-store'; +import thunk from 'redux-thunk'; +import { + GET_ORG_REPOS_LOADING, + GET_ORG_REPOS_ERROR, + GET_ORG_REPOS, +} from '../../../src/organization/organization.constants'; +import { fetchOrganizationRepos } from '../../../src/organization/organization.action'; +import { v3 } from '../../../src/api'; + +const store = configureStore([thunk])({ auth: { accessToken: 'ABCXYZ' } }); + +jest.mock('../../../src/api', () => ({ + v3: { + getJson: jest.fn(), + }, +})); + +describe('fetchOrganizationRepos()', () => { + afterEach(() => { + store.clearActions(); + }); + + it('should return a success response', async () => { + const expectedData = { name: 'organization' }; + + v3.getJson.mockResolvedValueOnce(expectedData); + await store.dispatch(fetchOrganizationRepos('')); + + expect(store.getActions()).toEqual( + expect.arrayContaining([ + { + type: GET_ORG_REPOS_LOADING, + payload: true, + }, + { + type: GET_ORG_REPOS_ERROR, + payload: '', + }, + { + type: GET_ORG_REPOS, + payload: expectedData, + }, + ]) + ); + + expect(store.getActions()).not.toEqual( + expect.arrayContaining([ + { + type: GET_ORG_REPOS_LOADING, + payload: false, + }, + { + type: GET_ORG_REPOS_ERROR, + payload: expectedData, + }, + ]) + ); + }); + + it('should return an error response', async () => { + const expectedData = { error: 'no organization' }; + + v3.getJson.mockRejectedValueOnce(expectedData); + await store.dispatch(fetchOrganizationRepos('')); + + expect(store.getActions()).toEqual( + expect.arrayContaining([ + { + type: GET_ORG_REPOS_LOADING, + payload: true, + }, + { + type: GET_ORG_REPOS_LOADING, + payload: false, + }, + { + type: GET_ORG_REPOS_ERROR, + payload: '', + }, + { + type: GET_ORG_REPOS_ERROR, + payload: expectedData, + }, + ]) + ); + + expect(store.getActions()).not.toEqual( + expect.arrayContaining([ + { + type: GET_ORG_REPOS, + payload: expectedData, + }, + ]) + ); + }); +}); diff --git a/__tests__/tests/actions/repository.action.js b/__tests__/tests/actions/repository.action.js new file mode 100644 index 000000000..e68609190 --- /dev/null +++ b/__tests__/tests/actions/repository.action.js @@ -0,0 +1,375 @@ +import configureStore from 'redux-mock-store'; +import thunk from 'redux-thunk'; +import { + GET_REPOSITORY_CONTENTS, + GET_REPOSITORY_FILE, + GET_REPOSITORY_COMMITS, + GET_COMMIT, + GET_COMMIT_DIFF, + GET_REPOSITORY_README, + GET_REPOSITORY_LABELS, +} from '../../../src/repository/repository.type'; +import { + getContents, + getRepositoryFile, + getCommits, + getCommitFromUrl, + getCommitDiffFromUrl, + getReadMe, + getLabels, + getCommitDetails, +} from '../../../src/repository/repository.action'; +import { fetchDiff, fetchReadMe, v3 } from '../../../src/api'; + +jest.mock('../../../src/api', () => ({ + v3: { + getJson: jest.fn(), + getRaw: jest.fn(), + }, + fetchDiff: jest.fn(), + fetchReadMe: jest.fn(), +})); + +const store = configureStore([thunk])({ auth: { accessToken: 'ABCXYZ' } }); + +beforeEach(() => { + jest.clearAllMocks(); +}); + +afterEach(() => { + store.clearActions(); +}); + +describe('getContents()', () => { + it('should return a success response', async () => { + const level = 'some-level'; + const expectedData = { key: 'value ' }; + + v3.getJson.mockResolvedValue(expectedData); + + await store.dispatch(getContents('', level)); + + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_CONTENTS.PENDING, + }); + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_CONTENTS.SUCCESS, + results: expectedData, + level, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_REPOSITORY_CONTENTS.ERROR, + }) + ); + }); + + it('should return an error response', async () => { + const expectedData = 'ERROR'; + + v3.getJson.mockRejectedValue(expectedData); + + await store.dispatch(getContents()); + + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_CONTENTS.PENDING, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_REPOSITORY_CONTENTS.SUCCESS, + }) + ); + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_CONTENTS.ERROR, + payload: expectedData, + }); + }); +}); + +describe('getRepositoryFile()', () => { + it('should return a success response', async () => { + const expectedData = { key: 'value ' }; + + v3.getRaw.mockResolvedValue(expectedData); + + await store.dispatch(getRepositoryFile('')); + + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_FILE.PENDING, + }); + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_FILE.SUCCESS, + payload: expectedData, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_REPOSITORY_FILE.ERROR, + }) + ); + }); + + it('should return an error response', async () => { + const expectedData = 'ERROR'; + + v3.getRaw.mockRejectedValue(expectedData); + + await store.dispatch(getRepositoryFile()); + + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_FILE.PENDING, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_REPOSITORY_FILE.SUCCESS, + }) + ); + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_FILE.ERROR, + payload: expectedData, + }); + }); +}); + +describe('getCommits()', () => { + it('should return a success response', async () => { + const expectedData = { key: 'value ' }; + + v3.getJson.mockResolvedValue(expectedData); + + await store.dispatch(getCommits('')); + + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_COMMITS.PENDING, + }); + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_COMMITS.SUCCESS, + payload: expectedData, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_REPOSITORY_COMMITS.ERROR, + }) + ); + }); + + it('should return an error response', async () => { + const expectedData = 'ERROR'; + + v3.getJson.mockRejectedValue(expectedData); + + await store.dispatch(getCommits()); + + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_COMMITS.PENDING, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_REPOSITORY_COMMITS.SUCCESS, + }) + ); + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_COMMITS.ERROR, + payload: expectedData, + }); + }); +}); + +describe('.getCommitFromUrl', () => { + it('should return a success response', async () => { + const expectedData = { key: 'value ' }; + + v3.getJson.mockResolvedValue(expectedData); + + await store.dispatch(getCommitFromUrl('')); + + expect(store.getActions()).toContainEqual({ + type: GET_COMMIT.PENDING, + }); + expect(store.getActions()).toContainEqual({ + type: GET_COMMIT.SUCCESS, + payload: expectedData, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_COMMIT.ERROR, + }) + ); + }); + + it('should return an error response', async () => { + const expectedData = 'ERROR'; + + v3.getJson.mockRejectedValue(expectedData); + + await store.dispatch(getCommitFromUrl()); + + expect(store.getActions()).toContainEqual({ + type: GET_COMMIT.PENDING, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_COMMIT.SUCCESS, + }) + ); + expect(store.getActions()).toContainEqual({ + type: GET_COMMIT.ERROR, + payload: expectedData, + }); + }); +}); + +describe('getCommitDiffFromUrl()', () => { + it('should return a success response', async () => { + const expectedData = { key: 'value ' }; + + fetchDiff.mockResolvedValue(expectedData); + + await store.dispatch(getCommitDiffFromUrl('')); + + expect(store.getActions()).toContainEqual({ + type: GET_COMMIT_DIFF.PENDING, + }); + expect(store.getActions()).toContainEqual({ + type: GET_COMMIT_DIFF.SUCCESS, + payload: expectedData, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_COMMIT_DIFF.ERROR, + }) + ); + }); + + it('should return an error response', async () => { + const expectedData = 'ERROR'; + + fetchDiff.mockRejectedValue(expectedData); + + await store.dispatch(getCommitDiffFromUrl()); + + expect(store.getActions()).toContainEqual({ + type: GET_COMMIT_DIFF.PENDING, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_COMMIT_DIFF.SUCCESS, + }) + ); + expect(store.getActions()).toContainEqual({ + type: GET_COMMIT_DIFF.ERROR, + payload: expectedData, + }); + }); +}); + +describe('getReadMe()', () => { + it('should return a success response', async () => { + const expectedData = { key: 'value ' }; + + fetchReadMe.mockResolvedValue(expectedData); + + await store.dispatch(getReadMe('')); + + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_README.PENDING, + }); + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_README.SUCCESS, + payload: expectedData, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_REPOSITORY_README.ERROR, + }) + ); + }); + + it('should return an error response', async () => { + const expectedData = 'ERROR'; + + fetchReadMe.mockRejectedValue(expectedData); + + await store.dispatch(getReadMe()); + + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_README.PENDING, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_REPOSITORY_README.SUCCESS, + }) + ); + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_README.ERROR, + payload: expectedData, + }); + }); +}); + +describe('getLabels()', () => { + it('should return a success response', async () => { + const expectedData = { key: 'value ' }; + + v3.getJson.mockResolvedValue(expectedData); + + await store.dispatch(getLabels('')); + + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_LABELS.PENDING, + }); + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_LABELS.SUCCESS, + payload: expectedData, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_REPOSITORY_LABELS.ERROR, + }) + ); + }); + + it('should return an error response', async () => { + const expectedData = 'ERROR'; + + v3.getJson.mockRejectedValue(expectedData); + + await store.dispatch(getLabels()); + + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_LABELS.PENDING, + }); + expect(store.getActions()).not.toContainEqual( + expect.objectContaining({ + type: GET_REPOSITORY_LABELS.SUCCESS, + }) + ); + expect(store.getActions()).toContainEqual({ + type: GET_REPOSITORY_LABELS.ERROR, + payload: expectedData, + }); + }); + + describe('getCommitDetails()', () => { + it('should get commit and commit diff', async () => { + const commit = { url: 'url.com' }; + + v3.getJson.mockResolvedValue({}); + + await store.dispatch(getCommitDetails(commit)); + + expect(v3.getJson).toHaveBeenCalledWith(commit.url, expect.any(String)); + expect(fetchDiff).toHaveBeenCalledWith(commit.url, expect.any(String)); + }); + + it('should get commit and commit diff when commit has nested commit', async () => { + const nestedCommit = { commit: { url: 'nestedUrl.dev' } }; + + v3.getJson.mockResolvedValue({}); + + await store.dispatch(getCommitDetails(nestedCommit)); + + expect(v3.getJson).toHaveBeenCalledWith(nestedCommit.commit.url, expect.any(String)); + expect(fetchDiff).toHaveBeenCalledWith(nestedCommit.commit.url, expect.any(String)); + }); + }); +}); diff --git a/package.json b/package.json index 48aa79f6b..0155ad657 100644 --- a/package.json +++ b/package.json @@ -148,6 +148,7 @@ "reactotron-react-native": "^1.14.0", "reactotron-redux": "^1.13.0", "recursive-keys": "^0.9.0", + "redux-mock-store": "^1.5.3", "stylelint": "^8.2.0", "stylelint-config-standard": "^18.0.0", "stylelint-config-styled-components": "^0.1.1", diff --git a/yarn.lock b/yarn.lock index 63f6ebbdf..1ed9b4017 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6846,6 +6846,11 @@ lodash.isempty@^4.4.0: resolved "https://registry.yarnpkg.com/lodash.isempty/-/lodash.isempty-4.4.0.tgz#6f86cbedd8be4ec987be9aaf33c9684db1b31e7e" integrity sha1-b4bL7di+TsmHvpqvM8loTbGzHn4= +lodash.isplainobject@^4.0.6: + version "4.0.6" + resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb" + integrity sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs= + lodash.map@^4.5.1: version "4.6.0" resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3" @@ -9696,6 +9701,13 @@ redux-logger@^2.7.4: dependencies: deep-diff "0.3.4" +redux-mock-store@^1.5.3: + version "1.5.3" + resolved "https://registry.yarnpkg.com/redux-mock-store/-/redux-mock-store-1.5.3.tgz#1f10528949b7ce8056c2532624f7cafa98576c6d" + integrity sha512-ryhkkb/4D4CUGpAV2ln1GOY/uh51aczjcRz9k2L2bPx/Xja3c5pSGJJPyR25GNVRXtKIExScdAgFdiXp68GmJA== + dependencies: + lodash.isplainobject "^4.0.6" + redux-persist-transform-encrypt@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/redux-persist-transform-encrypt/-/redux-persist-transform-encrypt-1.0.2.tgz#89e30aeeacb56567a0d0889d2f78403e8264ba22"