Skip to content

Commit

Permalink
update and add jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Liza K committed Apr 22, 2020
1 parent 1f7f788 commit 043e236
Showing 1 changed file with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ describe('Async search strategy', () => {

it('stops polling when the response is complete', async () => {
mockSearch
.mockReturnValueOnce(of({ id: 1, total: 2, loaded: 1 }))
.mockReturnValueOnce(of({ id: 1, total: 2, loaded: 2 }))
.mockReturnValueOnce(of({ id: 1, total: 2, loaded: 2 }));
.mockReturnValueOnce(of({ id: 1, total: 2, loaded: 1, is_running: true, is_partial: true }))
.mockReturnValueOnce(of({ id: 1, total: 2, loaded: 2, is_running: false, is_partial: false }))
.mockReturnValueOnce(
of({ id: 1, total: 2, loaded: 2, is_running: false, is_partial: false })
);

const asyncSearch = asyncSearchStrategyProvider({
core: mockCoreStart,
Expand All @@ -67,10 +69,39 @@ describe('Async search strategy', () => {
expect(mockSearch).toBeCalledTimes(2);
});

it('stops polling when the response is an error', async () => {
mockSearch
.mockReturnValueOnce(of({ id: 1, total: 2, loaded: 1, is_running: true, is_partial: true }))
.mockReturnValueOnce(of({ id: 1, total: 2, loaded: 2, is_running: false, is_partial: true }))
.mockReturnValueOnce(of({ id: 1, total: 2, loaded: 2, is_running: false, is_partial: true }));

const asyncSearch = asyncSearchStrategyProvider({
core: mockCoreStart,
getSearchStrategy: jest.fn().mockImplementation(() => {
return () => {
return {
search: mockSearch,
};
};
}),
});

expect(mockSearch).toBeCalledTimes(0);

await asyncSearch
.search(mockRequest, mockOptions)
.toPromise()
.catch(() => {
expect(mockSearch).toBeCalledTimes(2);
});
});

it('only sends the ID and server strategy after the first request', async () => {
mockSearch
.mockReturnValueOnce(of({ id: 1, total: 2, loaded: 1 }))
.mockReturnValueOnce(of({ id: 1, total: 2, loaded: 2 }));
.mockReturnValueOnce(of({ id: 1, total: 2, loaded: 1, is_running: true, is_partial: true }))
.mockReturnValueOnce(
of({ id: 1, total: 2, loaded: 2, is_running: false, is_partial: false })
);

const asyncSearch = asyncSearchStrategyProvider({
core: mockCoreStart,
Expand Down

0 comments on commit 043e236

Please sign in to comment.