Skip to content

Commit

Permalink
fix: service not accepted when no callbacks provided (fix #648)
Browse files Browse the repository at this point in the history
  • Loading branch information
orestbida committed Mar 3, 2024
1 parent e64b426 commit 9db6167
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
8 changes: 3 additions & 5 deletions src/utils/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
43 changes: 22 additions & 21 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

2 comments on commit 9db6167

@jonathan-lws
Copy link

@jonathan-lws jonathan-lws commented on 9db6167 Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, this doesn't seems to fix the issue.
Still able to reproduce it in OPT-IN mode.

@orestbida
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonathan-lws thanks for letting me know!

Please sign in to comment.