Skip to content

Commit

Permalink
change test to reflect new use of create api
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuellr committed Sep 21, 2020
1 parent 8917bcb commit 36283fb
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ async function update(success: boolean) {
expect(logger.error).lastCalledWith(
`alertsClient.update('alert-id') conflict, exceeded retries`
);
return expectConflict(success, err);
return expectConflict(success, err, 'create');
}
expectSuccess(success);
expectSuccess(success, 2, 'create');

// only checking the error messages in this test
expect(logger.warn).nthCalledWith(1, `alertsClient.update('alert-id') conflict, retrying ...`);
Expand Down Expand Up @@ -258,17 +258,21 @@ async function unmuteInstance(success: boolean) {
expectSuccess(success);
}

function expectSuccess(success: boolean, count: number = 2) {
function expectSuccess(
success: boolean,
count: number = 2,
method: 'update' | 'create' = 'update'
) {
expect(success).toBe(true);
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledTimes(count);
expect(unsecuredSavedObjectsClient[method]).toHaveBeenCalledTimes(count);
// message content checked in the update test
expect(logger.warn).toBeCalledTimes(1);
}

function expectConflict(success: boolean, err: Error) {
function expectConflict(success: boolean, err: Error, method: 'update' | 'create' = 'update') {
expect(`${err}`).toBe(`Error: ${ConflictErrorMessage}`);
expect(success).toBe(false);
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledTimes(ConflictAfterRetries);
expect(unsecuredSavedObjectsClient[method]).toHaveBeenCalledTimes(ConflictAfterRetries);
// message content checked in the update test
expect(logger.warn).toBeCalledTimes(RetryForConflictsAttempts);
expect(logger.error).toBeCalledTimes(1);
Expand All @@ -278,15 +282,20 @@ function testFn(fn: (success: boolean) => unknown, success: boolean) {
test(`${fn.name}`, async () => await fn(success));
}

// set up mocks for update or create (the update() method uses create!)
function mockSavedObjectUpdateConflictErrorTimes(times: number) {
// default success value
unsecuredSavedObjectsClient.update.mockResolvedValue(MockUpdateValue);
unsecuredSavedObjectsClient.create.mockResolvedValue(MockUpdateValue);

// queue up specified number of errors before a success call
for (let i = 0; i < times; i++) {
unsecuredSavedObjectsClient.update.mockRejectedValueOnce(
SavedObjectsErrorHelpers.createConflictError('alert', MockAlertId)
);
unsecuredSavedObjectsClient.create.mockRejectedValueOnce(
SavedObjectsErrorHelpers.createConflictError('alert', MockAlertId)
);
}
}

Expand Down

0 comments on commit 36283fb

Please sign in to comment.