Skip to content

Commit

Permalink
Merge pull request #698 from kethinov/more-tests
Browse files Browse the repository at this point in the history
added more tests
  • Loading branch information
kethinov authored Oct 20, 2024
2 parents b83b9ad + eff8c91 commit 1507bd5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
5 changes: 2 additions & 3 deletions cheerioPolyfill.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// create a native DOMParser
const parser = new window.DOMParser()

// stub out cheerio using native dom methods for frontend so we don't have to bundle cheerio on the frontend
export function load (html) {
// create a native DOMParser
const parser = new window.DOMParser()
const doc = parser.parseFromString(html, 'text/html')
doc.body.innerHTML = doc.head.innerHTML + doc.body.innerHTML

Expand Down
29 changes: 28 additions & 1 deletion test/loaders/playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@ function registerTemplates (dir) {
const templates = registerTemplates('test/templates')

function runPlaywrightAgainstTeddyBundle (teddyPath) {
playwrightTest.describe('Test that client-side bundles load', () => {
const teddyNonMinified = path.resolve(__dirname, '../../dist/teddy.js')
const teddyMinified = path.resolve(__dirname, '../../dist/teddy.min.js')

playwrightTest(`Load ${teddyNonMinified}`, async ({ page }) => {
// to debug, uncomment this:
// page.on('console', (msg) => console.log(msg))
// for deeper debugging: export DEBUG=pw:browser

await page.addScriptTag({ path: teddyNonMinified }) // add teddy script tag to the browser page
await page.evaluate(async () => {
if (!window?.teddy?.setTemplateRoot) throw new Error(`Assertion failed: expected ${teddyNonMinified} to load`)
})
})
playwrightTest(`Load ${teddyMinified}`, async ({ page }) => {
// to debug, uncomment this:
// page.on('console', (msg) => console.log(msg))
// for deeper debugging: export DEBUG=pw:browser

await page.addScriptTag({ path: teddyMinified }) // add teddy script tag to the browser page
await page.evaluate(async () => {
if (!window?.teddy?.setTemplateRoot) throw new Error(`Assertion failed: expected ${teddyMinified} to load`)
})
})
})

// run the main test suite
const fileName = teddyPath.split('/').pop()
for (const testGroup of testsToRun) {
playwrightTest.describe(`${testGroup.describe} (dist/${fileName})`, () => {
Expand Down Expand Up @@ -112,4 +139,4 @@ function runPlaywrightAgainstTeddyBundle (teddyPath) {
}

runPlaywrightAgainstTeddyBundle(path.resolve(__dirname, '../../dist/teddy.js'))
// runPlaywrightAgainstTeddyBundle(path.resolve(__dirname, '../../dist/teddy.min.js')) // uncomment to test the minified bundle too
// runPlaywrightAgainstTeddyBundle(path.resolve(__dirname, '../../dist/teddy.min.js')) // uncomment to run the test suite against the minified bundle too
56 changes: 56 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,62 @@ export default [
fs.rmSync('test/client.js')
},
expected: ''
},
{
message: 'should be able require teddy.client.cjs',
runMocha: async (teddy, template, model, assert, expected) => {
if (fs.existsSync('test/client.cjs')) fs.rmSync('test/client.cjs')

fs.writeFileSync('test/client.cjs', 'const teddy = require("../dist/teddy.client.cjs")\nconsole.log(teddy)')
const output = execSync('node ./test/client.cjs', { encoding: 'utf-8' }).toString()

assert(output.includes('emptyVarBehavior:'))

fs.rmSync('test/client.cjs')
},
expected: ''
},
{
message: 'should be able to import teddy.client.mjs',
runMocha: async (teddy, template, model, assert, expected) => {
if (fs.existsSync('test/client.js')) fs.rmSync('test/client.js')

fs.writeFileSync('test/client.js', 'import teddy from "../dist/teddy.client.mjs"\nconsole.log(teddy)')
const output = execSync('node ./test/client.js', { encoding: 'utf-8' }).toString()

assert(output.includes('emptyVarBehavior:'))

fs.rmSync('test/client.js')
},
expected: ''
},
{
message: 'should be able require teddy.min.cjs',
runMocha: async (teddy, template, model, assert, expected) => {
if (fs.existsSync('test/client.cjs')) fs.rmSync('test/client.cjs')

fs.writeFileSync('test/client.cjs', 'const teddy = require("../dist/teddy.min.cjs")\nconsole.log(teddy)')
const output = execSync('node ./test/client.cjs', { encoding: 'utf-8' }).toString()

assert(output.includes('emptyVarBehavior:'))

fs.rmSync('test/client.cjs')
},
expected: ''
},
{
message: 'should be able to import teddy.min.mjs',
runMocha: async (teddy, template, model, assert, expected) => {
if (fs.existsSync('test/client.js')) fs.rmSync('test/client.js')

fs.writeFileSync('test/client.js', 'import teddy from "../dist/teddy.min.mjs"\nconsole.log(teddy)')
const output = execSync('node ./test/client.js', { encoding: 'utf-8' }).toString()

assert(output.includes('emptyVarBehavior:'))

fs.rmSync('test/client.js')
},
expected: ''
}
]
}
Expand Down

0 comments on commit 1507bd5

Please sign in to comment.