This repository has been archived by the owner on Jun 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(app): improve sagas coverage (#1053)
- Loading branch information
1 parent
30f4acc
commit 2c9e6d4
Showing
37 changed files
with
943 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
packages/app/src/sagas/agreements/__tests__/load.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import axios from "axios"; | ||
import { runSaga } from "redux-saga"; | ||
|
||
import { initialState } from "../../../reducers/agreements"; | ||
import load from "../load"; | ||
|
||
jest.mock("axios"); | ||
const mockedAxios = /** @type {jest.Mocked<import("axios").AxiosInstance>} */ (axios); | ||
mockedAxios.create.mockReturnValue(mockedAxios); | ||
|
||
describe(`sagas/agreements/load()`, () => { | ||
let DISPATCHED; | ||
|
||
beforeEach(() => { | ||
DISPATCHED = []; | ||
}); | ||
|
||
describe(`with pageIndex=0`, () => { | ||
describe(`with query=""`, () => { | ||
it(`should behave as expected`, async () => { | ||
const data = [{ name: "An Agreement" }]; | ||
|
||
mockedAxios.get.mockResolvedValueOnce({ | ||
data, | ||
headers: { | ||
"content-range": "0-9/15", | ||
}, | ||
}); | ||
|
||
await runSaga( | ||
{ | ||
dispatch: action => DISPATCHED.push(action), | ||
getState: () => initialState, | ||
}, | ||
load, | ||
{ meta: { pageIndex: 0, query: "" } }, | ||
).toPromise(); | ||
|
||
expect(mockedAxios.get).toHaveBeenCalledTimes(1); | ||
expect(mockedAxios.get).toHaveBeenCalledWith( | ||
"/agreements?select=*&order=idcc.asc&limit=10&offset=0", | ||
{ | ||
headers: { Prefer: "count=exact" }, | ||
}, | ||
); | ||
|
||
expect(DISPATCHED).toHaveLength(1); | ||
expect(DISPATCHED[0]).toEqual({ | ||
payload: { list: data, pageIndex: 0, pagesLength: 2 }, | ||
type: "AGREEMENTS_LOAD_SUCCESS", | ||
}); | ||
}); | ||
}); | ||
|
||
describe(`with query="A Query"`, () => { | ||
it(`should behave as expected`, async () => { | ||
const data = [{ name: "An Agreement" }]; | ||
|
||
mockedAxios.get.mockResolvedValueOnce({ | ||
data, | ||
headers: { | ||
"content-range": "0-9/15", | ||
}, | ||
}); | ||
|
||
await runSaga( | ||
{ | ||
dispatch: action => DISPATCHED.push(action), | ||
getState: () => initialState, | ||
}, | ||
load, | ||
{ meta: { pageIndex: 0, query: "A Query" } }, | ||
).toPromise(); | ||
|
||
expect(mockedAxios.get).toHaveBeenCalledTimes(1); | ||
expect(mockedAxios.get).toHaveBeenCalledWith( | ||
"/agreements?select=*&name=ilike.*A Query*&order=idcc.asc&limit=10&offset=0", | ||
{ | ||
headers: { Prefer: "count=exact" }, | ||
}, | ||
); | ||
|
||
expect(DISPATCHED).toHaveLength(1); | ||
expect(DISPATCHED[0]).toEqual({ | ||
payload: { list: data, pageIndex: 0, pagesLength: 2 }, | ||
type: "AGREEMENTS_LOAD_SUCCESS", | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe(`with pageIndex=-1`, () => { | ||
describe(`with query=""`, () => { | ||
it(`should behave as expected`, async () => { | ||
const data = [{ name: "An Agreement" }]; | ||
|
||
mockedAxios.get.mockResolvedValueOnce({ | ||
data, | ||
headers: { | ||
"content-range": "0-14/15", | ||
}, | ||
}); | ||
|
||
await runSaga( | ||
{ | ||
dispatch: action => DISPATCHED.push(action), | ||
getState: () => initialState, | ||
}, | ||
load, | ||
{ meta: { pageIndex: -1, query: "" } }, | ||
).toPromise(); | ||
|
||
expect(mockedAxios.get).toHaveBeenCalledTimes(1); | ||
expect(mockedAxios.get).toHaveBeenCalledWith("/agreements?select=*&order=idcc.asc", { | ||
headers: { Prefer: "count=exact" }, | ||
}); | ||
|
||
expect(DISPATCHED).toHaveLength(1); | ||
expect(DISPATCHED[0]).toEqual({ | ||
payload: { list: data, pageIndex: -1, pagesLength: -1 }, | ||
type: "AGREEMENTS_LOAD_SUCCESS", | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import axios from "axios"; | ||
import { runSaga } from "redux-saga"; | ||
|
||
import getCurrentUser from "../../../libs/getCurrentUser"; | ||
import { initialState } from "../../../reducers/answers"; | ||
import { getAnswersFilters } from "../../../selectors"; | ||
import load from "../load"; | ||
|
||
jest.mock("axios"); | ||
const mockedAxios = /** @type {jest.Mocked<import("axios").AxiosInstance>} */ (axios); | ||
mockedAxios.create.mockReturnValue(mockedAxios); | ||
|
||
jest.mock("../../../libs/getCurrentUser"); | ||
jest.mock("../../../selectors"); | ||
|
||
describe(`sagas/answers/load()`, () => { | ||
const DATA = { | ||
answers: [ | ||
{ agreement_name: "An Agreement", id: "00000000-0000-4000-8000-000000000001" }, | ||
{ agreement_name: null, id: "00000000-0000-4000-8000-000000000002" }, | ||
], | ||
answersReferences: [], | ||
}; | ||
let DISPATCHED; | ||
let FILTERS; | ||
|
||
beforeEach(() => { | ||
DISPATCHED = []; | ||
|
||
getAnswersFilters.mockReturnValueOnce(FILTERS); | ||
}); | ||
|
||
describe(`when the user is a contributor`, () => { | ||
beforeAll(() => { | ||
FILTERS = initialState.filters; | ||
|
||
getCurrentUser.mockReturnValue({ | ||
agreements: [ | ||
"00000000-0000-4000-8000-000000000003", | ||
"00000000-0000-4000-8000-000000000004", | ||
], | ||
id: "00000000-0000-4000-8000-000000000005", | ||
role: "contributor", | ||
}); | ||
}); | ||
|
||
describe(`when the filter {state}=["todo", "draft]`, () => { | ||
it(`should behave as expected`, async () => { | ||
mockedAxios.get.mockResolvedValueOnce({ | ||
data: DATA.answers, | ||
headers: { | ||
"content-range": "0-9/15", | ||
}, | ||
}); | ||
mockedAxios.get.mockResolvedValueOnce({ | ||
data: DATA.answersReferences, | ||
headers: { | ||
"content-range": "0-14/15", | ||
}, | ||
}); | ||
|
||
await runSaga( | ||
{ | ||
dispatch: action => DISPATCHED.push(action), | ||
getState: () => initialState, | ||
}, | ||
load, | ||
{ meta: { pagesIndex: 0 } }, | ||
).toPromise(); | ||
|
||
expect(getAnswersFilters).toHaveBeenCalledTimes(1); | ||
|
||
expect(mockedAxios.get).toHaveBeenCalledTimes(2); | ||
expect(mockedAxios.get).toHaveBeenNthCalledWith( | ||
1, | ||
`/full_answers?select=*&state=in.(todo,draft,pending_review,under_review,validated)&agreement_id=in.("00000000-0000-4000-8000-000000000003","00000000-0000-4000-8000-000000000004")&order=question_index.asc,agreement_idcc.asc&limit=10&offset=0`, | ||
{ | ||
headers: { Prefer: "count=exact" }, | ||
}, | ||
); | ||
expect(mockedAxios.get).toHaveBeenNthCalledWith( | ||
2, | ||
`/answers_references?select=*&answer_id=in.(00000000-0000-4000-8000-000000000001,00000000-0000-4000-8000-000000000002)&order=category.asc,value.asc`, | ||
{}, | ||
); | ||
|
||
const expectedList = [ | ||
{ | ||
agreement_name: "An Agreement", | ||
id: "00000000-0000-4000-8000-000000000001", | ||
references: [], | ||
}, | ||
{ agreement_name: null, id: "00000000-0000-4000-8000-000000000002", references: [] }, | ||
]; | ||
|
||
expect(DISPATCHED).toHaveLength(1); | ||
expect(DISPATCHED[0]).toEqual({ | ||
payload: { length: 15, list: expectedList, pagesIndex: 0, pagesLength: 2 }, | ||
type: "ANSWERS_LOAD_SUCCESS", | ||
}); | ||
}); | ||
}); | ||
|
||
describe(`when the filter {state}=["draft]`, () => { | ||
FILTERS = { | ||
...initialState.filters, | ||
states: ["draft"], | ||
}; | ||
|
||
it(`should behave as expected`, async () => { | ||
mockedAxios.get.mockResolvedValueOnce({ | ||
data: DATA.answers, | ||
headers: { | ||
"content-range": "0-9/15", | ||
}, | ||
}); | ||
mockedAxios.get.mockResolvedValueOnce({ | ||
data: DATA.answersReferences, | ||
headers: { | ||
"content-range": "0-14/15", | ||
}, | ||
}); | ||
|
||
await runSaga( | ||
{ | ||
dispatch: action => DISPATCHED.push(action), | ||
getState: () => initialState, | ||
}, | ||
load, | ||
{ meta: { pagesIndex: 0 } }, | ||
).toPromise(); | ||
|
||
expect(getAnswersFilters).toHaveBeenCalledTimes(1); | ||
|
||
expect(mockedAxios.get).toHaveBeenCalledTimes(2); | ||
expect(mockedAxios.get).toHaveBeenNthCalledWith( | ||
1, | ||
`/full_answers?select=*&state=in.(todo,draft,pending_review,under_review,validated)&agreement_id=in.("00000000-0000-4000-8000-000000000003","00000000-0000-4000-8000-000000000004")&order=question_index.asc,agreement_idcc.asc&limit=10&offset=0`, | ||
{ | ||
headers: { Prefer: "count=exact" }, | ||
}, | ||
); | ||
expect(mockedAxios.get).toHaveBeenNthCalledWith( | ||
2, | ||
`/answers_references?select=*&answer_id=in.(00000000-0000-4000-8000-000000000001,00000000-0000-4000-8000-000000000002)&order=category.asc,value.asc`, | ||
{}, | ||
); | ||
|
||
const expectedList = [ | ||
{ | ||
agreement_name: "An Agreement", | ||
id: "00000000-0000-4000-8000-000000000001", | ||
references: [], | ||
}, | ||
{ agreement_name: null, id: "00000000-0000-4000-8000-000000000002", references: [] }, | ||
]; | ||
|
||
expect(DISPATCHED).toHaveLength(1); | ||
expect(DISPATCHED[0]).toEqual({ | ||
payload: { length: 15, list: expectedList, pagesIndex: 0, pagesLength: 2 }, | ||
type: "ANSWERS_LOAD_SUCCESS", | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.