From e682cfa0746d2010f80bfb6fd51264242802f0e6 Mon Sep 17 00:00:00 2001 From: Maciej Ziarkowski Date: Mon, 27 Jan 2020 01:06:58 +0000 Subject: [PATCH] Add edit history data access mock --- .../api/dataAccess/__mocks__/editHistory.ts | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 app/src/api/dataAccess/__mocks__/editHistory.ts diff --git a/app/src/api/dataAccess/__mocks__/editHistory.ts b/app/src/api/dataAccess/__mocks__/editHistory.ts new file mode 100644 index 00000000..ad84e63b --- /dev/null +++ b/app/src/api/dataAccess/__mocks__/editHistory.ts @@ -0,0 +1,67 @@ +import { EditHistoryEntry } from '../../../frontend/models/edit-history-entry'; +import { numDesc } from '../../../helpers'; + +const mockEditHistory = + jest.genMockFromModule('../editHistory') as typeof import('../editHistory') & { + __setHistory: (mockHistoryData: EditHistoryEntry[]) => void + }; + +let mockData: EditHistoryEntry[] = []; + +mockEditHistory.__setHistory = function(mockHistoryData: EditHistoryEntry[]) { + mockData = mockHistoryData; +}; + +mockEditHistory.getHistoryAfterId = function(id: string, count: number): Promise { + return Promise.resolve( + mockData + .filter(x => BigInt(x.revision_id) > BigInt(id)) + .sort(numDesc(x => BigInt(x.revision_id))) + .slice(0, count) + ); +}; + +mockEditHistory.getHistoryBeforeId = function(id: string, count: number): Promise { + return Promise.resolve( + mockData + .filter(x => BigInt(x.revision_id) < BigInt(id)) + .sort(numDesc(x => BigInt(x.revision_id))) + .slice(0, count) + ); +}; + +mockEditHistory.getLatestHistory = function(count: number): Promise { + return Promise.resolve( + mockData + .slice(mockData.length - count, mockData.length) + .sort(numDesc(x => BigInt(x.revision_id))) + ); +}; + +mockEditHistory.getIdNewerThan = async function(id: string): Promise { + const historyAfterId = await mockEditHistory.getHistoryAfterId(id, 1); + return historyAfterId[historyAfterId.length - 1]?.revision_id; +}; + +mockEditHistory.getIdOlderThan = async function(id: string): Promise { + const historyBeforeId = await mockEditHistory.getHistoryBeforeId(id, 1); + return historyBeforeId[0]?.revision_id; +}; + +const { + __setHistory, + getHistoryAfterId, + getHistoryBeforeId, + getLatestHistory, + getIdNewerThan, + getIdOlderThan +} = mockEditHistory; + +export { + __setHistory, + getHistoryAfterId, + getHistoryBeforeId, + getLatestHistory, + getIdNewerThan, + getIdOlderThan +};