Skip to content

Commit

Permalink
Tests for maybeUpdatePhishingList
Browse files Browse the repository at this point in the history
Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
  • Loading branch information
segun committed Nov 23, 2022
1 parent 763aab7 commit 96cc0ca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
50 changes: 50 additions & 0 deletions packages/phishing-controller/src/PhishingController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,56 @@ describe('PhishingController', () => {
expect(nockScope.isDone()).toBe(false);
});

describe('maybeUpdatePhishingLists', () => {
it('should not be out of date immediately after maybeUpdatePhishingLists is called', async () => {
const clock = sinon.useFakeTimers();
const controller = new PhishingController({ refreshInterval: 10 });
clock.tick(10);
expect(controller.isOutOfDate()).toBe(true);
await controller.maybeUpdatePhishingLists();
expect(controller.isOutOfDate()).toBe(false);
});

it('should not be out of date after maybeUpdatePhishingLists is called but before refresh interval has passed', async () => {
const clock = sinon.useFakeTimers();
const controller = new PhishingController({ refreshInterval: 10 });
clock.tick(10);
expect(controller.isOutOfDate()).toBe(true);
await controller.maybeUpdatePhishingLists();
clock.tick(5);
expect(controller.isOutOfDate()).toBe(false);
});

it('should still be out of date while update is in progress', async () => {
const clock = sinon.useFakeTimers();
const controller = new PhishingController({ refreshInterval: 10 });
clock.tick(10);
await controller.maybeUpdatePhishingLists();
expect(controller.isOutOfDate()).toBe(false);
clock.tick(10);
expect(controller.isOutOfDate()).toBe(true);
// do not wait
controller.maybeUpdatePhishingLists();
expect(controller.isOutOfDate()).toBe(true);
await controller.maybeUpdatePhishingLists();
expect(controller.isOutOfDate()).toBe(false);
clock.tick(10);
expect(controller.isOutOfDate()).toBe(true);
});

it('should not call update if it is out of date', async () => {
const clock = sinon.useFakeTimers();
const controller = new PhishingController({ refreshInterval: 10 });
const updateSpy = sinon.spy(controller, 'updatePhishingLists');
expect(controller.isOutOfDate()).toBe(false);
await controller.maybeUpdatePhishingLists();
expect(updateSpy.calledOnce).toBe(false);
clock.tick(10);
await controller.maybeUpdatePhishingLists();
expect(updateSpy.calledOnce).toBe(true);
});
});

describe('isOutOfDate', () => {
it('should not be out of date upon construction', () => {
sinon.useFakeTimers();
Expand Down
4 changes: 2 additions & 2 deletions packages/phishing-controller/src/PhishingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ export class PhishingController extends BaseController<
}
}

maybeUpdatePhishingLists() {
async maybeUpdatePhishingLists() {
const phishingListsAreOutOfDate = this.isOutOfDate();
if (phishingListsAreOutOfDate) {
this.updatePhishingLists();
await this.updatePhishingLists();
}
}

Expand Down

0 comments on commit 96cc0ca

Please sign in to comment.