-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) Offline Registered Patients in Offline Tools Card | Sync Queue…
… Functions for Retrieving Full Sync Item | Sync Queue Manipulation Tests (#402) * Precache importmap refs as part of the static dependency lifecycle. Remove obsolete XMLHttpRequest patches. * Updated translations. * Consider offline registered patients in the offline tools patient overview card. * Provide API for retrieving full sync items. * Export new full sync item API members. * Create test cases covering the manipulation of the sync queue. * Import fixup. * Code simplifications.
- Loading branch information
1 parent
dfe11f1
commit c66b9f1
Showing
5 changed files
with
216 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,7 @@ module.exports = { | |
transform: { | ||
"^.+\\.tsx?$": ["@swc/jest"], | ||
}, | ||
moduleNameMapper: { | ||
"lodash-es": "lodash", | ||
}, | ||
}; |
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,153 @@ | ||
import "fake-indexeddb/auto"; | ||
import { getLoggedInUser, LoggedInUser } from "@openmrs/esm-api"; | ||
import { | ||
getFullSynchronizationItems, | ||
getFullSynchronizationItemsFor, | ||
getOfflineDb, | ||
getSynchronizationItems, | ||
getSynchronizationItemsFor, | ||
QueueItemDescriptor, | ||
queueSynchronizationItem, | ||
queueSynchronizationItemFor, | ||
deleteSynchronizationItem, | ||
getSynchronizationItem, | ||
} from "./sync"; | ||
|
||
interface MockSyncItem { | ||
value: number; | ||
} | ||
|
||
const systemTime = new Date(); | ||
const mockUserId = "00000000-0000-0000-0000-000000000000"; | ||
const mockSyncItemType = "mock-sync-item"; | ||
const defaultMockSyncItem: MockSyncItem = { | ||
value: 123, | ||
}; | ||
const defaultMockSyncItemDescriptor: QueueItemDescriptor = { | ||
dependencies: [], | ||
id: "123", | ||
displayName: "Mock Sync Item", | ||
patientUuid: "00000000-0000-0000-0000-000000000001", | ||
}; | ||
|
||
jest.mock("@openmrs/esm-api", () => ({ | ||
getLoggedInUser: jest.fn(async () => ({ uuid: mockUserId })), | ||
})); | ||
|
||
afterEach(async () => { | ||
// We want each test case to start fresh with a clean sync queue. | ||
await getOfflineDb().syncQueue.clear(); | ||
}); | ||
|
||
describe("Sync Queue", () => { | ||
beforeAll(() => { | ||
// We want to control the timers to ensure that we can test the `createdOn` attribute | ||
// of the sync item (which is created using `new Date()`). | ||
jest.useFakeTimers("modern"); | ||
jest.setSystemTime(systemTime); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it("enqueues sync item with expected attributes", async () => { | ||
const id = await queueSynchronizationItemFor( | ||
mockUserId, | ||
mockSyncItemType, | ||
defaultMockSyncItem, | ||
defaultMockSyncItemDescriptor | ||
); | ||
const queuedItems = await getFullSynchronizationItemsFor<MockSyncItem>( | ||
mockUserId, | ||
mockSyncItemType | ||
); | ||
|
||
expect(queuedItems).toHaveLength(1); | ||
expect(queuedItems[0].id).toBe(id); | ||
expect(queuedItems[0].type).toBe(mockSyncItemType); | ||
expect(queuedItems[0].userId).toBe(mockUserId); | ||
expect(queuedItems[0].createdOn).toStrictEqual(systemTime); | ||
expect(queuedItems[0].content).toStrictEqual(defaultMockSyncItem); | ||
expect(queuedItems[0].descriptor).toStrictEqual( | ||
defaultMockSyncItemDescriptor | ||
); | ||
}); | ||
}); | ||
|
||
describe("Logged-in user specific functions", () => { | ||
it("enqueue and return sync items of currently logged-in user", async () => { | ||
const loggedInUserId = (await getLoggedInUser()).uuid; | ||
await queueSynchronizationItem(mockSyncItemType, defaultMockSyncItem); | ||
const queuedItems = await getFullSynchronizationItems(mockSyncItemType); | ||
|
||
expect(queuedItems).toHaveLength(1); | ||
expect(queuedItems[0].userId).toBe(loggedInUserId); | ||
}); | ||
}); | ||
|
||
describe("getSynchronizationItems", () => { | ||
it("returns `content` of corresponding `getFullSynchronizationItems` call", async () => { | ||
await queueSynchronizationItem(mockSyncItemType, defaultMockSyncItem); | ||
const items = await getSynchronizationItems(mockSyncItemType); | ||
const fullItems = await getFullSynchronizationItems(mockSyncItemType); | ||
expect(items).toHaveLength(1); | ||
expect(fullItems).toHaveLength(1); | ||
expect(items[0]).toStrictEqual(fullItems[0].content); | ||
}); | ||
}); | ||
|
||
describe("getSynchronizationItemsFor", () => { | ||
it("returns `content` of corresponding `getFullSynchronizationItemsFor` call", async () => { | ||
await queueSynchronizationItemFor( | ||
mockUserId, | ||
mockSyncItemType, | ||
defaultMockSyncItem | ||
); | ||
const items = await getSynchronizationItemsFor( | ||
mockUserId, | ||
mockSyncItemType | ||
); | ||
const fullItems = await getFullSynchronizationItemsFor( | ||
mockUserId, | ||
mockSyncItemType | ||
); | ||
|
||
expect(items).toHaveLength(1); | ||
expect(fullItems).toHaveLength(1); | ||
expect(items[0]).toStrictEqual(fullItems[0].content); | ||
}); | ||
}); | ||
|
||
describe("getSynchronizationItem", () => { | ||
it("returns the specific sync item with given ID", async () => { | ||
const id = await queueSynchronizationItem( | ||
mockSyncItemType, | ||
defaultMockSyncItem | ||
); | ||
const items = await getFullSynchronizationItems(mockSyncItemType); | ||
const item = await getSynchronizationItem(id); | ||
expect(item).toStrictEqual(items[0]); | ||
}); | ||
|
||
it("returns undefined when no item with given ID exists", async () => { | ||
const item = await getSynchronizationItem(404); | ||
expect(item).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe("deleteSynchronizationItem", () => { | ||
it("deletes sync item with given ID", async () => { | ||
const id = await queueSynchronizationItem( | ||
mockSyncItemType, | ||
defaultMockSyncItem | ||
); | ||
await deleteSynchronizationItem(id); | ||
const items = await getSynchronizationItems(mockSyncItemType); | ||
expect(items).toHaveLength(0); | ||
}); | ||
|
||
it("does not throw when no item with given ID exists", async () => { | ||
await deleteSynchronizationItem(404); | ||
}); | ||
}); |
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