import {html, fixture, expect, oneEvent, waitUntil} from '@open-wc/testing'; import type { ToujouThirdPartyContent } from '../src/toujou-third-party-content/toujou-third-party-content'; import '../src/toujou-consent/toujou-consent'; import '../src/toujou-third-party-content/toujou-third-party-content'; import {configureStore} from '../src/toujou-consent-widget/store'; import {AnyAction, applyMiddleware, Store} from 'redux'; describe('ToujouThirdPartyContent - Initial Rendering', () => { it('should render placeholder content by default when consent is not given', async () => { const testStore = configureStore({ consents: { video: { consentGiven: false } } }); const el = await fixture(html`

Placeholder

`); const placeholder = el.querySelector('[slot="placeholder"]'); const contentContainer = el.querySelector('.toujou-third-party-content__templated-content'); expect(placeholder).to.exist; expect(contentContainer?.innerHTML).to.be.empty; }); it('should render the actual content if consent is already given', async () => { const testStore = configureStore({ consents: { video: { consentGiven: true } } }); const el = await fixture(html`
`); const iframe = el.querySelector('.toujou-third-party-content__templated-content iframe'); expect(iframe).to.exist; expect(iframe?.src).to.equal('https://example.com/'); expect(el.hasAttribute('showingcontent')).to.be.true; }); it('should render the actual content if it is commented and if consent is already given', async () => { const testStore = configureStore({ consents: { video: { consentGiven: true } } }); const el = await fixture(html`
`); await el.updateComplete; const iframe = el.querySelector('.toujou-third-party-content__templated-content iframe'); expect(iframe).to.exist; expect(iframe?.src).to.equal('https://example.com/'); expect(el.hasAttribute('showingcontent')).to.be.true; }); }); describe('ToujouThirdPartyContent - User Interactions', () => { let testStore: Store; let el: ToujouThirdPartyContent; let actions: AnyAction[]; function logAction() { return next => action => { actions.push(action); return next(action); }; } beforeEach(async () => { actions = []; testStore = configureStore({consents: {video: {consentGiven: false}}}, [applyMiddleware(logAction)]); el = await fixture(html`
`); }); it('should show content temporarily when "show once" button is clicked', async () => { const showOnceButton = el.querySelector('[ttpc-showcontentonce]'); showOnceButton?.click(); await el.updateComplete; const iframe = el.querySelector('.toujou-third-party-content__templated-content iframe'); expect(iframe).to.exist; expect(actions.length).to.equal(1); }); it('should dispatch a "saveSingleConsent" action when "always allow" button is clicked', async () => { const consentButton = el.querySelector('.consent__button'); consentButton?.click(); await el.updateComplete; const iframe = el.querySelector('.toujou-third-party-content__templated-content iframe'); expect(iframe).to.exist; expect(actions[1].payload.consentType).to.equal('video'); expect(actions[1].payload.consentData.consentGiven).to.be.true; }); it('should dispatch a "toujou-add-snackbar" event on consent click', async () => { const consentButton = el.querySelector('.consent__button'); setTimeout(() => consentButton?.click()); const { detail } = await oneEvent(el, 'toujou-add-snackbar'); expect(detail.message).to.equal('video enabled!'); expect(detail.variant).to.equal('success'); }); }); describe('ToujouThirdPartyContent - Script Rendering', () => { interface TestEl extends ToujouThirdPartyContent { scriptHasLoaded: boolean; } interface TestScript extends HTMLScriptElement { scriptHasRun: boolean; } it('a script should be executed if its rendered', async () => { const testStore = configureStore({ consents: { tracking: { consentGiven: true } } }); const el = await fixture(html`
`); await el.updateComplete; const script = el.querySelector('.toujou-third-party-content__templated-content script'); expect(script).to.exist; expect(script.scriptHasRun).to.be.true; expect(el.hasAttribute('showingcontent')).to.be.true; }); it('a script should be loaded if its rendered', async () => { const testStore = configureStore({ consents: { tracking: { consentGiven: true } } }); const el = await fixture(html`
`) as TestEl; await el.updateComplete; await waitUntil(() => el.scriptHasLoaded, 'Script has not loaded'); const script = el.querySelector('.toujou-third-party-content__templated-content script'); expect(script).to.exist; expect(script.scriptHasRun).to.be.true; expect(el.hasAttribute('showingcontent')).to.be.true; expect(el.scriptHasLoaded).to.be.true; }); });