diff --git a/src/utils/scripts.js b/src/utils/scripts.js index cebcb2ba..c7cb3ee3 100644 --- a/src/utils/scripts.js +++ b/src/utils/scripts.js @@ -38,24 +38,22 @@ export const manageExistingScripts = (defaultEnabledCategories) => { if (!service) continue; - const {onAccept, onReject} = service; + const { onAccept, onReject } = service; if ( !service._enabled && elContains(_acceptedServices[categoryName], serviceName) - && isFunction(onAccept) ) { service._enabled = true; - onAccept(); + isFunction(onAccept) && onAccept(); } else if ( service._enabled && !elContains(_acceptedServices[categoryName], serviceName) - && isFunction(onReject) ) { service._enabled = false; - onReject(); + isFunction(onReject) && onReject(); } } } diff --git a/tests/api.test.js b/tests/api.test.js index 52b4042c..6cf523fe 100644 --- a/tests/api.test.js +++ b/tests/api.test.js @@ -346,54 +346,55 @@ describe("API tests", () => { expect(api.validCookie('service1Cookie2')).toBe(false); }) - it('Should call service onAccept and onReject when both defined', async () => { + it('Should mark service as accepted regardless of callback/script', 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; + testConfig.categories.analytics.services.service2.label = 'service2'; + testConfig.categories.analytics.services.service2.onReject = undefined; + testConfig.categories.analytics.services.service2.onReject = undefined; await api.run(testConfig); api.acceptService('service2', 'analytics'); - api.acceptService([], 'analytics'); - expect(onAccept).toHaveBeenCalledTimes(1); - expect(onReject).toHaveBeenCalledTimes(1); + expect(api.acceptedService('service2', 'analytics')).toBe(true); + }) + + it('Should mark service as rejected regardless of callback/script', async () => { + api.reset(true); + testConfig.categories.analytics.services.service2.label = 'service2'; + testConfig.categories.analytics.services.service2.onReject = undefined; + testConfig.categories.analytics.services.service2.onReject = undefined; + await api.run(testConfig); api.acceptService('service2', 'analytics'); + expect(api.acceptedService('service2', 'analytics')).toBe(true); api.acceptService([], 'analytics'); - expect(onAccept).toHaveBeenCalledTimes(2); - expect(onReject).toHaveBeenCalledTimes(2); + expect(api.acceptedService('service2', 'analytics')).toBe(false); }) - it('Should call service onAccept once when onReject not defined', async () => { + it('Should call service onAccept once', 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'); + api.acceptCategory('analytics'); expect(onAccept).toHaveBeenCalledTimes(1); }) - it('Should not call service onReject when onAccept not defined', async () => { + it('Should call service onReject once, if previously accepted', async () => { api.reset(true); const onReject = jest.fn(); - testConfig.categories.analytics.services.service2.onAccept = null; + testConfig.categories.analytics.services.service2.onAccept = undefined; 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.acceptCategory('all'); api.acceptService([], 'analytics'); - expect(onReject).toHaveBeenCalledTimes(0) + api.acceptCategory([]); + expect(onReject).toHaveBeenCalledTimes(1) }) }) \ No newline at end of file