Add functions for multiple boards to BoardService

(cherry picked from commit 9dbefac95a8b60b93ba7b05fd7bf69b80b395f54)
This commit is contained in:
Winnie Hellmann 2019-07-12 20:00:46 +00:00 committed by Mike Greiling
parent d32cdb840f
commit 822bb97457
3 changed files with 209 additions and 0 deletions

View file

@ -62,6 +62,22 @@ export default class BoardService {
static toggleIssueSubscription(endpoint) {
return boardsStore.toggleIssueSubscription(endpoint);
}
allBoards() {
return boardsStore.allBoards();
}
recentBoards() {
return boardsStore.recentBoards();
}
createBoard(board) {
return boardsStore.createBoard(board);
}
deleteBoard({ id }) {
return boardsStore.deleteBoard({ id });
}
}
window.BoardService = BoardService;

View file

@ -340,6 +340,40 @@ const boardsStore = {
toggleIssueSubscription(endpoint) {
return axios.post(endpoint);
},
allBoards() {
return axios.get(this.generateBoardsPath());
},
recentBoards() {
return axios.get(this.state.endpoints.recentBoardsEndpoint);
},
createBoard(board) {
const boardPayload = { ...board };
boardPayload.label_ids = (board.labels || []).map(b => b.id);
if (boardPayload.label_ids.length === 0) {
boardPayload.label_ids = [''];
}
if (boardPayload.assignee) {
boardPayload.assignee_id = boardPayload.assignee.id;
}
if (boardPayload.milestone) {
boardPayload.milestone_id = boardPayload.milestone.id;
}
if (boardPayload.id) {
return axios.put(this.generateBoardsPath(boardPayload.id), { board: boardPayload });
}
return axios.post(this.generateBoardsPath(), { board: boardPayload });
},
deleteBoard({ id }) {
return axios.delete(this.generateBoardsPath(id));
},
};
BoardsStoreEE.initEESpecific(boardsStore);

View file

@ -389,4 +389,163 @@ describe('BoardService', () => {
return expect(BoardService.toggleIssueSubscription(dummyEndpoint)).rejects.toThrow();
});
});
describe('allBoards', () => {
const url = `${endpoints.boardsEndpoint}.json`;
it('makes a request to fetch all boards', () => {
axiosMock.onGet(url).replyOnce(200, dummyResponse);
const expectedResponse = expect.objectContaining({ data: dummyResponse });
return expect(service.allBoards()).resolves.toEqual(expectedResponse);
});
it('fails for error response', () => {
axiosMock.onGet(url).replyOnce(500);
return expect(service.allBoards()).rejects.toThrow();
});
});
describe('recentBoards', () => {
const url = `${endpoints.recentBoardsEndpoint}.json`;
it('makes a request to fetch all boards', () => {
axiosMock.onGet(url).replyOnce(200, dummyResponse);
const expectedResponse = expect.objectContaining({ data: dummyResponse });
return expect(service.recentBoards()).resolves.toEqual(expectedResponse);
});
it('fails for error response', () => {
axiosMock.onGet(url).replyOnce(500);
return expect(service.recentBoards()).rejects.toThrow();
});
});
describe('createBoard', () => {
const labelIds = ['first label', 'second label'];
const assigneeId = 'as sign ee';
const milestoneId = 'vegetable soup';
const board = {
labels: labelIds.map(id => ({ id })),
assignee: { id: assigneeId },
milestone: { id: milestoneId },
};
describe('for existing board', () => {
const id = 'skate-board';
const url = `${endpoints.boardsEndpoint}/${id}.json`;
const expectedRequest = expect.objectContaining({
data: JSON.stringify({
board: {
...board,
id,
label_ids: labelIds,
assignee_id: assigneeId,
milestone_id: milestoneId,
},
}),
});
let requestSpy;
beforeEach(() => {
requestSpy = jest.fn();
axiosMock.onPut(url).replyOnce(config => requestSpy(config));
});
it('makes a request to update the board', () => {
requestSpy.mockReturnValue([200, dummyResponse]);
const expectedResponse = expect.objectContaining({ data: dummyResponse });
return expect(
service.createBoard({
...board,
id,
}),
)
.resolves.toEqual(expectedResponse)
.then(() => {
expect(requestSpy).toHaveBeenCalledWith(expectedRequest);
});
});
it('fails for error response', () => {
requestSpy.mockReturnValue([500]);
return expect(
service.createBoard({
...board,
id,
}),
)
.rejects.toThrow()
.then(() => {
expect(requestSpy).toHaveBeenCalledWith(expectedRequest);
});
});
});
describe('for new board', () => {
const url = `${endpoints.boardsEndpoint}.json`;
const expectedRequest = expect.objectContaining({
data: JSON.stringify({
board: {
...board,
label_ids: labelIds,
assignee_id: assigneeId,
milestone_id: milestoneId,
},
}),
});
let requestSpy;
beforeEach(() => {
requestSpy = jest.fn();
axiosMock.onPost(url).replyOnce(config => requestSpy(config));
});
it('makes a request to create a new board', () => {
requestSpy.mockReturnValue([200, dummyResponse]);
const expectedResponse = expect.objectContaining({ data: dummyResponse });
return expect(service.createBoard(board))
.resolves.toEqual(expectedResponse)
.then(() => {
expect(requestSpy).toHaveBeenCalledWith(expectedRequest);
});
});
it('fails for error response', () => {
requestSpy.mockReturnValue([500]);
return expect(service.createBoard(board))
.rejects.toThrow()
.then(() => {
expect(requestSpy).toHaveBeenCalledWith(expectedRequest);
});
});
});
});
describe('deleteBoard', () => {
const id = 'capsized';
const url = `${endpoints.boardsEndpoint}/${id}.json`;
it('makes a request to delete a boards', () => {
axiosMock.onDelete(url).replyOnce(200, dummyResponse);
const expectedResponse = expect.objectContaining({ data: dummyResponse });
return expect(service.deleteBoard({ id })).resolves.toEqual(expectedResponse);
});
it('fails for error response', () => {
axiosMock.onDelete(url).replyOnce(500);
return expect(service.deleteBoard({ id })).rejects.toThrow();
});
});
});