Skip to content

Commit

Permalink
[@dhealthdapps/backend] feat(widgets): fix of tests for validate chal…
Browse files Browse the repository at this point in the history
…lenge scheduler
  • Loading branch information
kravchenkodhealth authored and evias committed Jan 3, 2023
1 parent bc8ea8e commit 8e6f528
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class ValidateChallengeScheduler {
} catch (err) {
// if challenge isn't on chain - print info to the console
this.logger.error("failed to validate challenge", err);
throw err;
// throw err;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
* @license LGPL-3.0
*/

const jobStartCall = jest.fn();
const jobStopCall = jest.fn();
jest.mock("cron", () => {
return {
CronJob: jest.fn().mockImplementation(() => {
return { start: jobStartCall, stop: jobStopCall };
}),
};
});

// external dependencies
import { TestingModule, Test } from "@nestjs/testing";
import { SchedulerRegistry } from "@nestjs/schedule";
Expand All @@ -17,7 +27,10 @@ import { EventEmitter2 } from "@nestjs/event-emitter";
import { getModelToken } from "@nestjs/mongoose";

// internal dependencies
import { AuthService } from "../../../../src/common/services/AuthService";
import {
AuthService,
AuthenticationPayload,
} from "../../../../src/common/services/AuthService";
import { NetworkService } from "../../../../src/common/services/NetworkService";
import { AccountsService } from "../../../../src/common/services/AccountsService";
import { ChallengesService } from "../../../../src/common/services/ChallengesService";
Expand All @@ -27,6 +40,7 @@ import { ValidateChallengeScheduler } from "../../../../src/common/schedulers/Va

describe("common/ValidateChallengeScheduler", () => {
let validateChallengeScheduler: ValidateChallengeScheduler;
let authService: AuthService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand Down Expand Up @@ -59,6 +73,7 @@ describe("common/ValidateChallengeScheduler", () => {
validateChallengeScheduler = module.get<ValidateChallengeScheduler>(
ValidateChallengeScheduler,
);
authService = module.get<AuthService>(AuthService);
});

it("should be defined", () => {
Expand Down Expand Up @@ -145,7 +160,6 @@ describe("common/ValidateChallengeScheduler", () => {
});

it("should clear stopTimeout", () => {
const clearTimeout = jest.fn();
(validateChallengeScheduler as any).startCronJob();
(validateChallengeScheduler as any).stopCronJob();

Expand All @@ -156,31 +170,54 @@ describe("common/ValidateChallengeScheduler", () => {
});

describe("validate()", () => {
it("should call validateChallenge", () => {
const mockedValidate = jest.fn();
it("should call validateChallenge", async () => {
const mockedValidate = jest
.spyOn(authService, "validateChallenge")
.mockResolvedValue({} as AuthenticationPayload);

(validateChallengeScheduler as any).authService = {
validateChallenge: mockedValidate,
};

(validateChallengeScheduler as any).validate();
await (validateChallengeScheduler as any).validate();

expect(mockedValidate).toBeCalled();
});

it("should log error if challenge invalid", () => {
it("should log error if challenge invalid", async () => {
(validateChallengeScheduler as any).challenge = "invalidChallenge";
jest
.spyOn(authService, "validateChallenge")
.mockRejectedValue(new Error("error"));
const mockedFn = jest.fn();
(validateChallengeScheduler as any).logger = {
error: mockedFn,
};

const result = (validateChallengeScheduler as any).validate();

expect(result).rejects.toThrow();
});

it("should stop cronJob", () => {
const mockedFn = jest.fn();
(validateChallengeScheduler as any).stopCronJob = mockedFn;
jest.spyOn(authService, "validateChallenge").mockResolvedValue(null);

(validateChallengeScheduler as any).validate();

expect((validateChallengeScheduler as any).validate()).toThrowError();
expect(jobStopCall).toBeCalled();
});

// it("should stop cronJob", () => {
// const mockedFn = jest.fn();
// (validateChallengeScheduler as any).stopCronJob = mockedFn;
it("should emit event", async () => {
const mockedFn = jest.fn();
jest
.spyOn(authService, "validateChallenge")
.mockResolvedValue({} as AuthenticationPayload);

(validateChallengeScheduler as any).emitter = {
emit: mockedFn,
};

// (validateChallengeScheduler as any).validate();
await (validateChallengeScheduler as any).validate();

// expect(mockedFn).toBeCalled();
// });
expect(mockedFn).toBeCalled();
});
});
});

0 comments on commit 8e6f528

Please sign in to comment.