From dca0cdb5819b118a9f5571a750852c7ce975e91e Mon Sep 17 00:00:00 2001 From: dwyfrequency Date: Fri, 14 Apr 2023 21:14:59 -0400 Subject: [PATCH] Prevent Trusted Types tests to run when the API does not exist --- packages/analytics/src/helpers.test.ts | 83 +++++++++++++------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/packages/analytics/src/helpers.test.ts b/packages/analytics/src/helpers.test.ts index 98df87b6c04..9d79c6b3a4a 100644 --- a/packages/analytics/src/helpers.test.ts +++ b/packages/analytics/src/helpers.test.ts @@ -51,54 +51,55 @@ const fakeDynamicConfig: DynamicConfig = { const fakeDynamicConfigPromises = [Promise.resolve(fakeDynamicConfig)]; describe('Trusted Types policies and functions', () => { - describe('Trusted types exists', () => { - let ttStub: SinonStub; - - beforeEach(() => { - ttStub = stub( - window.trustedTypes as TrustedTypePolicyFactory, - 'createPolicy' - ).returns({ - createScriptURL: (s: string) => s - } as any); - }); + if (window.trustedTypes) { + describe('Trusted types exists', () => { + let ttStub: SinonStub; + + beforeEach(() => { + ttStub = stub( + window.trustedTypes as TrustedTypePolicyFactory, + 'createPolicy' + ).returns({ + createScriptURL: (s: string) => s + } as any); + }); - afterEach(() => { - removeGtagScripts(); - ttStub.restore(); - }); + afterEach(() => { + removeGtagScripts(); + ttStub.restore(); + }); - it('Verify trustedTypes is called if the API is available', () => { - const trustedTypesPolicy = createTrustedTypesPolicy( - 'firebase-js-sdk-policy', - { - createScriptURL: createGtagTrustedTypesScriptURL - } - ); + it('Verify trustedTypes is called if the API is available', () => { + const trustedTypesPolicy = createTrustedTypesPolicy( + 'firebase-js-sdk-policy', + { + createScriptURL: createGtagTrustedTypesScriptURL + } + ); - expect(ttStub).to.be.called; - expect(trustedTypesPolicy).not.to.be.undefined; - }); + expect(ttStub).to.be.called; + expect(trustedTypesPolicy).not.to.be.undefined; + }); - it('createGtagTrustedTypesScriptURL verifies gtag URL base exists when a URL is provided', () => { - expect(createGtagTrustedTypesScriptURL(GTAG_URL)).to.equal(GTAG_URL); - }); + it('createGtagTrustedTypesScriptURL verifies gtag URL base exists when a URL is provided', () => { + expect(createGtagTrustedTypesScriptURL(GTAG_URL)).to.equal(GTAG_URL); + }); - it('createGtagTrustedTypesScriptURL rejects URLs with non-gtag base', () => { - const NON_GTAG_URL = 'http://iamnotgtag.com'; - const loggerWarnStub = stub(logger, 'warn'); - const errorMessage = ERROR_FACTORY.create( - AnalyticsError.INVALID_GTAG_RESOURCE, - { - gtagURL: NON_GTAG_URL - } - ).message; + it('createGtagTrustedTypesScriptURL rejects URLs with non-gtag base', () => { + const NON_GTAG_URL = 'http://iamnotgtag.com'; + const loggerWarnStub = stub(logger, 'warn'); + const errorMessage = ERROR_FACTORY.create( + AnalyticsError.INVALID_GTAG_RESOURCE, + { + gtagURL: NON_GTAG_URL + } + ).message; - expect(createGtagTrustedTypesScriptURL(NON_GTAG_URL)).to.equal(''); - expect(loggerWarnStub).to.be.calledWith(errorMessage); + expect(createGtagTrustedTypesScriptURL(NON_GTAG_URL)).to.equal(''); + expect(loggerWarnStub).to.be.calledWith(errorMessage); + }); }); - }); - + } describe('Trusted types does not exist', () => { it('Verify trustedTypes functions are not called if the API is not available', () => { delete window.trustedTypes;