Skip to content

Commit

Permalink
Merge pull request #594 from techfg/fix-service-callbacks
Browse files Browse the repository at this point in the history
add tests for service callbacks
  • Loading branch information
orestbida authored Nov 30, 2023
2 parents 9bd84ca + 1da031c commit d502966
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions tests/api.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as CookieConsent from "../src/index"
import testConfig from "./config/full-config";
import { getKeys } from "../src/utils/general";
import { globalObj } from "../src/core/global";
import { htmlHasClass, setCookie } from "./config/mocks-utils";
import { htmlHasClass } from "./config/mocks-utils";

/**
* @type {import("../src/core/global").Api}
Expand All @@ -20,6 +18,7 @@ global.fetch = jest.fn(() =>
);

describe("API tests", () =>{
let testConfig;

beforeAll(async () => {

Expand All @@ -34,6 +33,10 @@ describe("API tests", () =>{
})

beforeEach(async () => {
await jest.isolateModulesAsync(async () => {
const mod = await import('./config/full-config');
testConfig = mod.default;
})
await api.run(testConfig);
});

Expand Down Expand Up @@ -342,4 +345,55 @@ describe("API tests", () =>{
expect(api.validCookie('service1Cookie1')).toBe(false);
expect(api.validCookie('service1Cookie2')).toBe(false);
})

it('Should call service onAccept and onReject when both defined', async () => {
api.reset(true);
const onAccept = jest.fn();
const onReject = jest.fn();
testConfig.categories.analytics.services.service2.onAccept = onAccept;
testConfig.categories.analytics.services.service2.onReject = onReject;
await api.run(testConfig);

api.acceptService('service2', 'analytics');
api.acceptService([], 'analytics');
expect(onAccept).toHaveBeenCalledTimes(1);
expect(onReject).toHaveBeenCalledTimes(1);

api.acceptService('service2', 'analytics');
api.acceptService([], 'analytics');
expect(onAccept).toHaveBeenCalledTimes(2);
expect(onReject).toHaveBeenCalledTimes(2);
})

it('Should call service onAccept once when onReject not defined', async () => {
api.reset(true);
const onAccept = jest.fn();
testConfig.categories.analytics.services.service2.onAccept = onAccept;
testConfig.categories.analytics.services.service2.onReject = null;
await api.run(testConfig);

api.acceptService('service2', 'analytics');
api.acceptService([], 'analytics');
expect(onAccept).toHaveBeenCalledTimes(1);

api.acceptService('service2', 'analytics');
api.acceptService([], 'analytics');
expect(onAccept).toHaveBeenCalledTimes(1);
})

it('Should not call service onReject when onAccept not defined', async () => {
api.reset(true);
const onReject = jest.fn();
testConfig.categories.analytics.services.service2.onAccept = null;
testConfig.categories.analytics.services.service2.onReject = onReject;
await api.run(testConfig);

api.acceptService('service2', 'analytics');
api.acceptService([], 'analytics');
expect(onReject).toHaveBeenCalledTimes(0)

api.acceptService('service2', 'analytics');
api.acceptService([], 'analytics');
expect(onReject).toHaveBeenCalledTimes(0)
})
})

0 comments on commit d502966

Please sign in to comment.