Skip to content

Commit a059af0

Browse files
authored
Prevent Trusted Types tests to run when the API does not exist (#7230)
1 parent a57a2b5 commit a059af0

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

packages/analytics/src/helpers.test.ts

+42-41
Original file line numberDiff line numberDiff line change
@@ -51,54 +51,55 @@ const fakeDynamicConfig: DynamicConfig = {
5151
const fakeDynamicConfigPromises = [Promise.resolve(fakeDynamicConfig)];
5252

5353
describe('Trusted Types policies and functions', () => {
54-
describe('Trusted types exists', () => {
55-
let ttStub: SinonStub;
56-
57-
beforeEach(() => {
58-
ttStub = stub(
59-
window.trustedTypes as TrustedTypePolicyFactory,
60-
'createPolicy'
61-
).returns({
62-
createScriptURL: (s: string) => s
63-
} as any);
64-
});
54+
if (window.trustedTypes) {
55+
describe('Trusted types exists', () => {
56+
let ttStub: SinonStub;
57+
58+
beforeEach(() => {
59+
ttStub = stub(
60+
window.trustedTypes as TrustedTypePolicyFactory,
61+
'createPolicy'
62+
).returns({
63+
createScriptURL: (s: string) => s
64+
} as any);
65+
});
6566

66-
afterEach(() => {
67-
removeGtagScripts();
68-
ttStub.restore();
69-
});
67+
afterEach(() => {
68+
removeGtagScripts();
69+
ttStub.restore();
70+
});
7071

71-
it('Verify trustedTypes is called if the API is available', () => {
72-
const trustedTypesPolicy = createTrustedTypesPolicy(
73-
'firebase-js-sdk-policy',
74-
{
75-
createScriptURL: createGtagTrustedTypesScriptURL
76-
}
77-
);
72+
it('Verify trustedTypes is called if the API is available', () => {
73+
const trustedTypesPolicy = createTrustedTypesPolicy(
74+
'firebase-js-sdk-policy',
75+
{
76+
createScriptURL: createGtagTrustedTypesScriptURL
77+
}
78+
);
7879

79-
expect(ttStub).to.be.called;
80-
expect(trustedTypesPolicy).not.to.be.undefined;
81-
});
80+
expect(ttStub).to.be.called;
81+
expect(trustedTypesPolicy).not.to.be.undefined;
82+
});
8283

83-
it('createGtagTrustedTypesScriptURL verifies gtag URL base exists when a URL is provided', () => {
84-
expect(createGtagTrustedTypesScriptURL(GTAG_URL)).to.equal(GTAG_URL);
85-
});
84+
it('createGtagTrustedTypesScriptURL verifies gtag URL base exists when a URL is provided', () => {
85+
expect(createGtagTrustedTypesScriptURL(GTAG_URL)).to.equal(GTAG_URL);
86+
});
8687

87-
it('createGtagTrustedTypesScriptURL rejects URLs with non-gtag base', () => {
88-
const NON_GTAG_URL = 'http://iamnotgtag.com';
89-
const loggerWarnStub = stub(logger, 'warn');
90-
const errorMessage = ERROR_FACTORY.create(
91-
AnalyticsError.INVALID_GTAG_RESOURCE,
92-
{
93-
gtagURL: NON_GTAG_URL
94-
}
95-
).message;
88+
it('createGtagTrustedTypesScriptURL rejects URLs with non-gtag base', () => {
89+
const NON_GTAG_URL = 'http://iamnotgtag.com';
90+
const loggerWarnStub = stub(logger, 'warn');
91+
const errorMessage = ERROR_FACTORY.create(
92+
AnalyticsError.INVALID_GTAG_RESOURCE,
93+
{
94+
gtagURL: NON_GTAG_URL
95+
}
96+
).message;
9697

97-
expect(createGtagTrustedTypesScriptURL(NON_GTAG_URL)).to.equal('');
98-
expect(loggerWarnStub).to.be.calledWith(errorMessage);
98+
expect(createGtagTrustedTypesScriptURL(NON_GTAG_URL)).to.equal('');
99+
expect(loggerWarnStub).to.be.calledWith(errorMessage);
100+
});
99101
});
100-
});
101-
102+
}
102103
describe('Trusted types does not exist', () => {
103104
it('Verify trustedTypes functions are not called if the API is not available', () => {
104105
delete window.trustedTypes;

0 commit comments

Comments
 (0)