Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

Commit e47453e

Browse files
committed
Add tests
1 parent c88a7a6 commit e47453e

4 files changed

Lines changed: 54 additions & 6 deletions

File tree

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"no-invalid-this": "off",
88
"@typescript-eslint/no-invalid-this": ["error"],
99
"import/extensions": ["error", "always"],
10-
"github/no-inner-html": "off"
10+
"github/no-inner-html": "off",
11+
"@typescript-eslint/no-unused-vars": ["error", { "vars": "all", "argsIgnorePattern": "^_" }]
1112
},
1213
"overrides": [
1314
{

test/render.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expect} from 'chai'
2-
import {html, render} from '../lib/index.js'
2+
import {html, render, setCSPTrustedTypesPolicy} from '../lib/index.js'
33
import type {TemplateResult} from '../lib/index.js'
44

55
describe('render', () => {
@@ -8,6 +8,10 @@ describe('render', () => {
88
surface = document.createElement('section')
99
})
1010

11+
afterEach(() => {
12+
setCSPTrustedTypesPolicy(null)
13+
})
14+
1115
it('memoizes by TemplateResult#template, updating old templates with new values', () => {
1216
const main = (x: string | null = null) => html`<div class="${x}"></div>`
1317
render(main('foo'), surface)
@@ -55,4 +59,21 @@ describe('render', () => {
5559
expect(surface.innerHTML).to.contain('<div><span><div></div></span><span><div></div></span></div>')
5660
})
5761
})
62+
63+
describe('trusted types', () => {
64+
it('respects a Trusted Types Policy if it is set', () => {
65+
let policyCalled = false
66+
const rewrittenFragment = '<div id="bar"></div>'
67+
setCSPTrustedTypesPolicy({
68+
createHTML: (_html: string) => {
69+
policyCalled = true
70+
return rewrittenFragment
71+
}
72+
})
73+
const main = (x: string | null = null) => html`<div class="${x}"></div>`
74+
render(main('foo'), surface)
75+
expect(surface.innerHTML).to.equal(rewrittenFragment)
76+
expect(policyCalled).to.be.true
77+
})
78+
})
5879
})

test/trusted-types.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import {expect} from 'chai'
2-
import {TrustedTypesPolicy} from '../lib/index.js'
2+
import {setCSPTrustedTypesPolicy} from '../lib/index.js'
3+
import {getCSPTrustedTypesPolicy} from '../lib/trusted-types.js'
34

45
describe('trusted types', () => {
6+
after(() => {
7+
setCSPTrustedTypesPolicy(null)
8+
})
9+
510
it('can set a CSP Trusted Types policy', () => {
611
const dummyPolicy = {
712
createHTML: (htmlText: string) => {
813
return htmlText
914
}
1015
}
11-
TrustedTypesPolicy.setTrustedTypesPolicy(dummyPolicy)
12-
expect(TrustedTypesPolicy.cspTrustedTypesPolicy).to.equal(dummyPolicy)
16+
expect(getCSPTrustedTypesPolicy()).to.equal(null)
17+
setCSPTrustedTypesPolicy(dummyPolicy)
18+
expect(getCSPTrustedTypesPolicy()).to.equal(dummyPolicy)
1319
})
1420
})

test/unsafe-html.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import {expect} from 'chai'
2-
import {html, render, unsafeHTML} from '../lib/index.js'
2+
import {html, render, setCSPTrustedTypesPolicy, unsafeHTML} from '../lib/index.js'
33

44
describe('unsafeHTML', () => {
5+
beforeEach(() => {
6+
setCSPTrustedTypesPolicy(null)
7+
})
8+
afterEach(() => {
9+
setCSPTrustedTypesPolicy(null)
10+
})
511
it('renders basic text', async () => {
612
const surface = document.createElement('section')
713
render(html`<div>${unsafeHTML('Hello World')}</div>`, surface)
@@ -31,4 +37,18 @@ describe('unsafeHTML', () => {
3137
render(fn('<a href="">Universe</a>'), surface)
3238
expect(surface.innerHTML).to.equal('<div><span>Hello</span><span><a href="">Universe</a></span></div>')
3339
})
40+
it('respects trusted types', async () => {
41+
let policyCalled = false
42+
const rewrittenFragment = '<div id="bar">This has been rewritten by Trusted Types.</div>'
43+
setCSPTrustedTypesPolicy({
44+
createHTML: (_html: string) => {
45+
policyCalled = true
46+
return rewrittenFragment
47+
}
48+
})
49+
const surface = document.createElement('section')
50+
render(html`<div>${unsafeHTML('<span>Hello</span><span>World</span>')}</div>`, surface)
51+
expect(surface.innerHTML).to.equal(rewrittenFragment)
52+
expect(policyCalled).to.be.true
53+
})
3454
})

0 commit comments

Comments
 (0)