Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
feat(sourcepoint): add consentReady state to vuex store
Browse files Browse the repository at this point in the history
  • Loading branch information
André Niendorf committed Oct 1, 2020
1 parent 43d1699 commit 705a57c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
28 changes: 23 additions & 5 deletions src/vue/vuex/sourcepoint/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mutations, actions } from './index';
import { getCustomVendorConsents } from '../../../sourcepoint';
import { Commit } from 'vuex';
import { Commit, Dispatch } from 'vuex';
import { SourcepointModuleState } from '../typings';
import { CustomPurpose, CustomVendor, CustomVendorConsentsResult } from '../../../sourcepoint/typings';
import { loadVendorPurposeMapping, getGrantedVendors } from '../../../vendor-mapping';
Expand All @@ -14,6 +14,7 @@ const createState = (args: Partial<SourcepointModuleState> = {}): SourcepointMod
consentedCustomVendors: [],
consentedCustomPurposes: [],
grantedVendors: [],
consentReady: false,
};

return { ...defaultState, ...args };
Expand Down Expand Up @@ -53,10 +54,25 @@ describe('vuex-module', () => {

expect(state.grantedVendors).toEqual([grant2]);
});

it('setConsentReady should set state property consentReady', () => {
const state = createState();

mutations.setConsentReady(state, true);

expect(state.consentReady).toEqual(true);
});
});

describe('actions', () => {
it('bootstrapConsents should setup the store', async () => {
it('bootstrapConsents should dispatch updateConsents', async () => {
const dispatch: Dispatch = jest.fn();

await actions.bootstrapConsents({ dispatch }, { propertyId: 1 });

expect(dispatch).toHaveBeenCalled();
});
it('updateConsents should setup the store', async () => {
const consents: CustomVendorConsentsResult = {
consentedPurposes: [],
consentedVendors: [],
Expand All @@ -70,14 +86,15 @@ describe('vuex-module', () => {
(getCustomVendorConsents as jest.Mock).mockImplementation(() => Promise.resolve(consents));
(getGrantedVendors as jest.Mock).mockImplementation(() => []);

await actions.bootstrapConsents({ commit }, { propertyId: 1 });
await actions.updateConsents({ commit });

expect(commit).toHaveBeenNthCalledWith(1, 'setCustomVendorConsents', consents.consentedVendors);
expect(commit).toHaveBeenNthCalledWith(2, 'setCustomPurposeConsents', consents.consentedPurposes);
expect(commit).toHaveBeenNthCalledWith(3, 'setGrantedVendors', []);
expect(commit).toHaveBeenNthCalledWith(4, 'setConsentReady', true);
});

it('bootstrapConsents should setup the store with default values', async () => {
it('updateConsents should setup the store with default values', async () => {
const consents: CustomVendorConsentsResult = {} as CustomVendorConsentsResult;

const vendorPurposeMappings: VendorPurposeMappings = [];
Expand All @@ -88,11 +105,12 @@ describe('vuex-module', () => {
(getCustomVendorConsents as jest.Mock).mockImplementation(() => Promise.resolve(consents));
(getGrantedVendors as jest.Mock).mockImplementation(() => []);

await actions.bootstrapConsents({ commit }, { propertyId: 1 });
await actions.updateConsents({ commit });

expect(commit).toHaveBeenNthCalledWith(1, 'setCustomVendorConsents', []);
expect(commit).toHaveBeenNthCalledWith(2, 'setCustomPurposeConsents', []);
expect(commit).toHaveBeenNthCalledWith(3, 'setGrantedVendors', []);
expect(commit).toHaveBeenNthCalledWith(4, 'setConsentReady', true);
});
});
});
13 changes: 11 additions & 2 deletions src/vue/vuex/sourcepoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Commit, Module } from 'vuex';
import { Commit, Dispatch, Module } from 'vuex';
import { getCustomVendorConsents } from '../../../sourcepoint';
import { SourcepointModuleState } from '../typings';
import { CustomPurpose, CustomVendor } from '../../../sourcepoint/typings';
Expand All @@ -13,6 +13,7 @@ const moduleState: SourcepointModuleState = {
consentedCustomVendors: [],
consentedCustomPurposes: [],
grantedVendors: [],
consentReady: false,
};

export const mutations = {
Expand All @@ -25,6 +26,9 @@ export const mutations = {
setGrantedVendors(state: SourcepointModuleState, payload: string[]): void {
state.grantedVendors = payload;
},
setConsentReady(state: SourcepointModuleState, payload: boolean): void {
state.consentReady = payload;
},
};

export const getters = {
Expand All @@ -33,7 +37,7 @@ export const getters = {
};

export const actions = {
async bootstrapConsents({ commit }: { commit: Commit }, payload: { propertyId: number }): Promise<void> {
async updateConsents({ commit }: { commit: Commit }): Promise<void> {
const { consentedPurposes = [], consentedVendors = [], grants = {} } = await getCustomVendorConsents();

configureGrants(grants);
Expand All @@ -42,6 +46,11 @@ export const actions = {
commit('setCustomPurposeConsents', consentedPurposes);
commit('setGrantedVendors', getGrantedVendors());

commit('setConsentReady', true);
},
async bootstrapConsents({ dispatch }: { dispatch: Dispatch }, payload: { propertyId: number }): Promise<void> {
await dispatch('updateConsents');

const mappings = await loadVendorPurposeMapping(payload.propertyId);

configureVendorPurposeMapping(mappings);
Expand Down
1 change: 1 addition & 0 deletions src/vue/vuex/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export type SourcepointModuleState = {
consentedCustomVendors: CustomVendor[];
consentedCustomPurposes: CustomPurpose[];
grantedVendors: string[];
consentReady: boolean;
};

0 comments on commit 705a57c

Please sign in to comment.