Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for service callbacks #594

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})