Skip to content

Commit

Permalink
feat(add-consent): add missing tests for consent
Browse files Browse the repository at this point in the history
  • Loading branch information
UralKrc committed Apr 10, 2024
1 parent 06d2c4c commit c5b80a1
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/consent/get-all-checkboxes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getAllCheckboxes } from './get-all-checkboxes';

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

beforeAll(() => {
windowObj = window;
});

afterAll(() => {
window = windowObj;
});

describe('getAllCheckboxes()', () => {
it('should return all checkboxes from zaraz consent', () => {
const checkboxesMock = { key1: true, key2: false };
window.zaraz = {
consent: {
getAllCheckboxes: jest.fn().mockReturnValue(checkboxesMock),
},
};

const checkboxes = getAllCheckboxes();

expect(checkboxes).toEqual(checkboxesMock);
});
});
33 changes: 33 additions & 0 deletions src/consent/get-all.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getAll } from './get-all';

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

beforeAll(() => {
windowObj = window;
});

afterAll(() => {
window = windowObj;
});

describe('getAll()', () => {
it('should return all consents from zaraz consent', () => {
const consentsMock = { key1: true, key2: false };
window.zaraz = {
consent: {
getAll: jest.fn().mockReturnValue(consentsMock),
},
};

const consents = getAll();

expect(consents).toEqual(consentsMock);
});
});
36 changes: 36 additions & 0 deletions src/consent/get-purposes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getPurposes } from './get-purposes';

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

beforeAll(() => {
windowObj = window;
});

afterAll(() => {
window = windowObj;
});

describe('getPurposes()', () => {
it('should return all purposes from zaraz consent', () => {
const purposesMock = {
key1: { name: 'Name 1', description: 'Description 1', order: 1 },
key2: { name: 'Name 2', description: 'Description 2', order: 2 },
};
window.zaraz = {
consent: {
purposes: purposesMock,
},
};

const purposes = getPurposes();

expect(purposes).toEqual(purposesMock);
});
});
34 changes: 34 additions & 0 deletions src/consent/get.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { get } from './get';

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

beforeAll(() => {
windowObj = window;
});

afterAll(() => {
window = windowObj;
});

describe('get()', () => {
it('should return the consent status for a purpose from zaraz consent', () => {
const getMock = jest.fn().mockReturnValue(true);
window.zaraz = {
consent: {
get: getMock,
},
};

const consentStatus = get('key');

expect(consentStatus).toBe(true);
expect(getMock).toHaveBeenCalledWith('key');
});
});
33 changes: 33 additions & 0 deletions src/consent/send-queued-event.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { sendQueuedEvents } from './send-queued-events';

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

beforeAll(() => {
windowObj = window;
});

afterAll(() => {
window = windowObj;
});

describe('sendQueuedEvents()', () => {
it('should call sendQueuedEvents method on zaraz consent', () => {
const sendQueuedEventsMock = jest.fn();
window.zaraz = {
consent: {
sendQueuedEvents: sendQueuedEventsMock,
},
};

sendQueuedEvents();

expect(sendQueuedEventsMock).toHaveBeenCalled();
});
});
33 changes: 33 additions & 0 deletions src/consent/set-all-checkboxes.spect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { setAllCheckboxes } from './set-all-checkboxes';

Check warning on line 1 in src/consent/set-all-checkboxes.spect.ts

View check run for this annotation

Codecov / codecov/patch

src/consent/set-all-checkboxes.spect.ts#L1

Added line #L1 was not covered by tests

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

beforeAll(() => {
windowObj = window;

Check warning on line 13 in src/consent/set-all-checkboxes.spect.ts

View check run for this annotation

Codecov / codecov/patch

src/consent/set-all-checkboxes.spect.ts#L12-L13

Added lines #L12 - L13 were not covered by tests
});

afterAll(() => {
window = windowObj;

Check warning on line 17 in src/consent/set-all-checkboxes.spect.ts

View check run for this annotation

Codecov / codecov/patch

src/consent/set-all-checkboxes.spect.ts#L16-L17

Added lines #L16 - L17 were not covered by tests
});

describe('setAllCheckboxes()', () => {
it('should call setAllCheckboxes method on zaraz consent with the correct argument', () => {
const setAllCheckboxesMock = jest.fn();
window.zaraz = {

Check warning on line 23 in src/consent/set-all-checkboxes.spect.ts

View check run for this annotation

Codecov / codecov/patch

src/consent/set-all-checkboxes.spect.ts#L20-L23

Added lines #L20 - L23 were not covered by tests
consent: {
setAllCheckboxes: setAllCheckboxesMock,
},
};

setAllCheckboxes(true);

Check warning on line 29 in src/consent/set-all-checkboxes.spect.ts

View check run for this annotation

Codecov / codecov/patch

src/consent/set-all-checkboxes.spect.ts#L29

Added line #L29 was not covered by tests

expect(setAllCheckboxesMock).toHaveBeenCalledWith(true);

Check warning on line 31 in src/consent/set-all-checkboxes.spect.ts

View check run for this annotation

Codecov / codecov/patch

src/consent/set-all-checkboxes.spect.ts#L31

Added line #L31 was not covered by tests
});
});
33 changes: 33 additions & 0 deletions src/consent/set-all.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { setAll } from './set-all';

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

beforeAll(() => {
windowObj = window;
});

afterAll(() => {
window = windowObj;
});

describe('setAll()', () => {
it('should call setAll method on zaraz consent with the correct argument', () => {
const setAllMock = jest.fn();
window.zaraz = {
consent: {
setAll: setAllMock,
},
};

setAll(true);

expect(setAllMock).toHaveBeenCalledWith(true);
});
});
34 changes: 34 additions & 0 deletions src/consent/set-checkboxes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { setCheckboxes } from './set-checkboxes';

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

beforeAll(() => {
windowObj = window;
});

afterAll(() => {
window = windowObj;
});

describe('setCheckboxes()', () => {
it('should call setCheckboxes method on zaraz consent with the correct argument', () => {
const setCheckboxesMock = jest.fn();
window.zaraz = {
consent: {
setCheckboxes: setCheckboxesMock,
},
};

const checkboxesStatus = { key1: true, key2: false };
setCheckboxes(checkboxesStatus);

expect(setCheckboxesMock).toHaveBeenCalledWith(checkboxesStatus);
});
});
34 changes: 34 additions & 0 deletions src/consent/set.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { set } from './set';

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

beforeAll(() => {
windowObj = window;
});

afterAll(() => {
window = windowObj;
});

describe('set()', () => {
it('should call set method on zaraz consent with the correct argument', () => {
const setMock = jest.fn();
window.zaraz = {
consent: {
set: setMock,
},
};

const consentPreferences = { key1: true, key2: false };
set(consentPreferences);

expect(setMock).toHaveBeenCalledWith(consentPreferences);
});
});

0 comments on commit c5b80a1

Please sign in to comment.