import { fixture, html, expect } from '@open-wc/testing'; import sinon from 'sinon'; import type { ReactiveControllerHost } from 'lit'; import { ToujouThirdPartyTemplateController } from '../src/toujou-third-party-content/toujou-third-party-template-controller'; // We import the module to stub the class within it import { TemplateRenderer } from '../src/utils/TemplateRenderer'; describe('ToujouThirdPartyTemplateController', () => { let host: ReactiveControllerHost; let renderIntoSpy: sinon.SinonSpy; let templateRendererConstructorSpy: { new(templateElement: HTMLTemplateElement): TemplateRenderer } & sinon.SinonSpy ; beforeEach(() => { // Mock the host component that the controller attaches to host = { addController: sinon.spy(), removeController: sinon.spy(), requestUpdate: sinon.spy(), updateComplete: sinon.promise() }; renderIntoSpy = sinon.spy((range: Range) => { // Simulate rendering by inserting a simple node range.insertNode(document.createTextNode('Mock Rendered Content')); }); // TODO figure out how to type this propery so ts-ignore is not necessary // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore templateRendererConstructorSpy = sinon.spy((...args: [HTMLTemplateElement]) => new (class extends TemplateRenderer { renderInto(range: Range) { renderIntoSpy(range); } })(...args) ); }); afterEach(() => { // Restore all stubs and spies after each test sinon.restore(); }); // Test Suite for the constructor and initialization describe('Initialization', () => { it('should add itself to the host on construction', () => { new ToujouThirdPartyTemplateController(host, templateRendererConstructorSpy); expect((host.addController as sinon.SinonSpy).calledOnce).to.be.true; }); }); // Test Suite for the core rendering logic describe('onConsentsChanged', () => { it('should do nothing if consent is not an object', async () => { await fixture(html` `); const controller = new ToujouThirdPartyTemplateController(host, templateRendererConstructorSpy); controller.onConsentsChanged({ video: false }); expect(templateRendererConstructorSpy.called).to.be.false; }); it('should do nothing if consentGiven is false', async () => { await fixture(html` `); const controller = new ToujouThirdPartyTemplateController(host, templateRendererConstructorSpy); controller.onConsentsChanged({ video: { consentGiven: false, consentCreationDate: 0, consentExpirationDate: 0, consentLifetime: 0, consentBoxDismissed: true } }); expect(templateRendererConstructorSpy.called).to.be.false; }); it('should render a matching template when consent is given', async () => { const container = await fixture(html`
Some text
`); const controller = new ToujouThirdPartyTemplateController(host, templateRendererConstructorSpy); controller.onConsentsChanged({ tracking: { consentGiven: true, consentCreationDate: 0, consentExpirationDate: 0, consentLifetime: 0, consentBoxDismissed: true } }); expect(templateRendererConstructorSpy.callCount).to.equal(2); expect(renderIntoSpy.callCount).to.equal(2); }); it('should only render templates for consented types', async () => { const container = await fixture(html`