Skip to content

Commit

Permalink
10553: Add unit test for "getUserByIdOnceAllUpdatesComplete";
Browse files Browse the repository at this point in the history
  • Loading branch information
John Cruz committed Dec 12, 2024
1 parent 7ed70d7 commit 4df64be
Showing 1 changed file with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { UserRecord } from '@web-api/persistence/dynamo/dynamoTypes';
import { applicationContext } from '@shared/business/test/createTestApplicationContext';
import { getUserByIdOnceAllUpdatesComplete } from '@web-api/persistence/dynamo/users/getUserByIdOnceAllUpdatesComplete';

describe('getUserByIdOnceAllUpdatesComplete', () => {
const TEST_USER_ID = 'TEST_USER_ID';
let RESOLVER: Function;

beforeEach(() => {
applicationContext
.getPersistenceGateway()
.getUserById.mockImplementation(
() => new Promise(resolve => (RESOLVER = resolve)),
);
});

it('should wait until the user is done updating to return the user record', async () => {
let COMPLETE_FLAG = false;

void getUserByIdOnceAllUpdatesComplete({
applicationContext,
userId: TEST_USER_ID,
}).then(userInfo => {
expect(userInfo).toEqual({ isUpdatingInformation: false });
COMPLETE_FLAG = true;
});

let getUserByIdCalls =
applicationContext.getPersistenceGateway().getUserById.mock.calls;
expect(getUserByIdCalls.length).toEqual(1);
RESOLVER({ isUpdatingInformation: true } as UserRecord);
await sleep(50);
expect(COMPLETE_FLAG).toEqual(false);

getUserByIdCalls =
applicationContext.getPersistenceGateway().getUserById.mock.calls;
expect(getUserByIdCalls.length).toEqual(2);
RESOLVER({ isUpdatingInformation: true } as UserRecord);
await sleep(50);
expect(COMPLETE_FLAG).toEqual(false);

getUserByIdCalls =
applicationContext.getPersistenceGateway().getUserById.mock.calls;
expect(getUserByIdCalls.length).toEqual(3);
RESOLVER({ isUpdatingInformation: false } as UserRecord);
await sleep(50);
expect(COMPLETE_FLAG).toEqual(true);
});
});

function sleep(timeInMilliseconds: number) {
return new Promise(resolve => {
setTimeout(() => {
resolve(null);
}, timeInMilliseconds);
});
}

0 comments on commit 4df64be

Please sign in to comment.