index.test.ts
This file
Path
src/helper/html/index.test.ts
78 lines · 2.5 KiB · TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 1why, one step back1import { html, raw } from '.'chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
line 2why, one step back2import { HtmlEscapedCallbackPhase, resolveCallback } from '../../utils/html'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 3–78why, one step back34describe('Tagged Template Literals', () => {5 it('Should escape special characters', () => {6 const name = 'John "Johnny" Smith'78 expect(html`<p>I'm ${name}.</p>`.toString()).toBe("<p>I'm John "Johnny" Smith.</p>")9 })1011 describe('Booleans, Null, and Undefined Are Ignored', () => {12 it.each([true, false, undefined, null])('%s', (item) => {13 expect(html`${item}`.toString()).toBe('')14 })1516 it('falsy value', () => {17 expect(html`${0}`.toString()).toBe('0')18 })19 })2021 it('Should call $array.flat(Infinity)', () => {22 const values = [23 'Name:',24 ['John "Johnny" Smith', undefined, null],25 ' Contact:',26 [html`<a href="http://example.com/">My Website</a>`],27 ]28 expect(html`<p>${values}</p>`.toString()).toBe(29 '<p>Name:John "Johnny" Smith Contact:<a href="http://example.com/">My Website</a></p>'30 )31 })3233 describe('Promise', () => {34 it('Should return Promise<string> when some variables contains Promise<string> in variables', async () => {35 const name = Promise.resolve('John "Johnny" Smith')36 const res = html`<p>I'm ${name}.</p>`37 expect(res).toBeInstanceOf(Promise)3839 expect((await res).toString()).toBe("<p>I'm John "Johnny" Smith.</p>")40 })4142 it('Should return raw value when some variables contains Promise<HtmlEscapedString> in variables', async () => {43 const name = Promise.resolve(raw('John "Johnny" Smith'))44 const res = html`<p>I'm ${name}.</p>`45 expect(res).toBeInstanceOf(Promise)46 expect((await res).toString()).toBe('<p>I\'m John "Johnny" Smith.</p>')47 })48 })4950 describe('HtmlEscapedString', () => {51 it('Should preserve callbacks', async () => {52 const name = raw('Hono', [53 ({ buffer }) => {54 if (buffer) {55 buffer[0] = buffer[0].replace('Hono', 'Hono!')56 }57 return undefined58 },59 ])60 const res = html`<p>I'm ${name}.</p>`61 expect(res).toBeInstanceOf(Promise)6263 expect((await res).toString()).toBe("<p>I'm Hono.</p>")64 expect(await resolveCallback(await res, HtmlEscapedCallbackPhase.Stringify, false, {})).toBe(65 "<p>I'm Hono!.</p>"66 )67 })68 })69})7071describe('raw', () => {72 it('Should be marked as escaped.', () => {73 const name = 'John "Johnny" Smith'74 expect(html`<p>I'm ${raw(name)}.</p>`.toString()).toBe(75 "<p>I'm John "Johnny" Smith.</p>"76 )77 })78})