Skip to content

Commit

Permalink
Add test coverage for CSP-<style> integration
Browse files Browse the repository at this point in the history
Follow-up to [basecamp#1151][]. Add unit test coverage for the
`installDefaultCSSForTagName` functions support for both `meta[content]`
and `meta[nonce]` attribute support.

[basecamp#1151]: basecamp#1151
  • Loading branch information
seanpdoyle committed Nov 20, 2024
1 parent d34ebea commit e6cbae4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "test/unit/composition_test"
import "test/unit/document_test"
import "test/unit/document_view_test"
import "test/unit/html_parser_test"
import "test/unit/helpers/custom_elements_test"
import "test/unit/location_mapper_test"
import "test/unit/mutation_observer_test"
import "test/unit/serialization_test"
Expand Down
55 changes: 55 additions & 0 deletions src/test/unit/helpers/custom_elements_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { assert, test, testGroup } from "test/test_helper"
import { installDefaultCSSForTagName, makeElement } from "trix/core/helpers"

let meta = null

const insertMeta = (attributes) => {
meta = makeElement("meta", attributes)
document.head.append(meta)
}

const installDefaultCSS = (tagName, css) => {
installDefaultCSSForTagName(tagName, css)

return document.querySelector(`style[data-tag-name=${tagName}]`)
}

const teardown = () => meta.remove()

testGroup("Helpers: Custom Elements", { teardown }, () => {
test("reads from meta[name=csp-nonce][content]", () => {
insertMeta({ name: "csp-nonce", content: "abc123" })

const style = installDefaultCSS("trix-element", "trix-element { display: block; }")

assert.equal(style.getAttribute("nonce"), "abc123")
assert.equal(style.innerHTML, "trix-element { display: block; }")
})

test("reads from meta[name=trix-csp-nonce][content]", () => {
insertMeta({ name: "trix-csp-nonce", content: "abc123" })

const style = installDefaultCSS("trix-element", "trix-element { display: block; }")

assert.equal(style.getAttribute("nonce"), "abc123")
assert.equal(style.innerHTML, "trix-element { display: block; }")
})

test("reads from meta[name=csp-nonce][nonce]", () => {
insertMeta({ name: "csp-nonce", nonce: "abc123" })

const style = installDefaultCSS("trix-element", "trix-element { display: block; }")

assert.equal(style.getAttribute("nonce"), "abc123")
assert.equal(style.innerHTML, "trix-element { display: block; }")
})

test("reads from meta[name=trix-csp-nonce][nonce]", () => {
insertMeta({ name: "trix-csp-nonce", nonce: "abc123" })

const style = installDefaultCSS("trix-element", "trix-element { display: block; }")

assert.equal(style.getAttribute("nonce"), "abc123")
assert.equal(style.innerHTML, "trix-element { display: block; }")
})
})

0 comments on commit e6cbae4

Please sign in to comment.