From 3619e0e578dcd398ca7a8e06d45373eae39e8f8a Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Fri, 29 Dec 2023 14:24:29 +0800 Subject: [PATCH] test(shared): improve unit tests coverage --- packages/shared/tests/dedupeHead.spec.ts | 128 ++++---- .../shared/tests/ensureEndingSlash.spec.ts | 12 +- .../shared/tests/ensureLeadingSlash.spec.ts | 12 +- .../shared/tests/formatDateString.spec.ts | 20 +- packages/shared/tests/isLinkExternal.spec.ts | 18 +- packages/shared/tests/isLinkHttp.spec.ts | 12 +- .../shared/tests/isLinkWithProtocol.spec.ts | 12 +- packages/shared/tests/isPlainObject.spec.ts | 10 +- .../shared/tests/removeEndingSlash.spec.ts | 12 +- .../shared/tests/removeLeadingSlash.spec.ts | 12 +- .../tests/resolveHeadIdentifier.spec.ts | 292 +++++++++--------- .../shared/tests/resolveLocalePath.spec.ts | 12 +- .../tests/resolveRoutePathFromUrl.spec.ts | 16 +- 13 files changed, 269 insertions(+), 299 deletions(-) diff --git a/packages/shared/tests/dedupeHead.spec.ts b/packages/shared/tests/dedupeHead.spec.ts index b40d9ba083..a73fae6b26 100644 --- a/packages/shared/tests/dedupeHead.spec.ts +++ b/packages/shared/tests/dedupeHead.spec.ts @@ -1,71 +1,69 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { dedupeHead } from '../src/index.js' import type { HeadConfig } from '../src/index.js' -describe('shared > dedupeHead', () => { - it('should dedupe head correctly', () => { - const head: HeadConfig[] = [ - // 1 - ['title', {}, 'foo'], - ['title', {}, 'bar'], - // 1 - ['base', { href: 'foo' }], - ['base', { href: 'bar' }], - // 2 - ['meta', { name: 'foo', content: 'foo' }], - ['meta', { name: 'foo', content: 'bar' }], - ['meta', { name: 'bar', content: 'bar' }], - // 2 - ['template', { id: 'foo' }, 'foo'], - ['template', { id: 'foo' }, 'bar'], - ['template', { id: 'bar' }, 'bar'], - // 3 - ['script', { src: 'foo' }], - ['script', { src: 'foo' }], - ['script', { src: 'bar' }], - ['script', { src: 'bar', async: true }], - // 2 - ['noscript', {}, 'foo'], - ['noscript', {}, 'foo'], - ['noscript', {}, 'bar'], - // 3 - ['link', { rel: 'icon', href: 'foo.ico' }], - ['link', { rel: 'icon', href: 'foo.ico' }], - ['link', { rel: 'stylesheet', href: 'foo.css' }], - ['link', { rel: 'canonical', href: 'foo' }], - // 2 - ['style', { type: 'text/css' }, 'body { color: red; }'], - ['style', { type: 'text/css' }, 'body { color: red; }'], - ['style', { type: 'text/css' }, 'body { color: white; }'], - ] +it('should dedupe head correctly', () => { + const head: HeadConfig[] = [ + // 1 + ['title', {}, 'foo'], + ['title', {}, 'bar'], + // 1 + ['base', { href: 'foo' }], + ['base', { href: 'bar' }], + // 2 + ['meta', { name: 'foo', content: 'foo' }], + ['meta', { name: 'foo', content: 'bar' }], + ['meta', { name: 'bar', content: 'bar' }], + // 2 + ['template', { id: 'foo' }, 'foo'], + ['template', { id: 'foo' }, 'bar'], + ['template', { id: 'bar' }, 'bar'], + // 3 + ['script', { src: 'foo' }], + ['script', { src: 'foo' }], + ['script', { src: 'bar' }], + ['script', { src: 'bar', async: true }], + // 2 + ['noscript', {}, 'foo'], + ['noscript', {}, 'foo'], + ['noscript', {}, 'bar'], + // 3 + ['link', { rel: 'icon', href: 'foo.ico' }], + ['link', { rel: 'icon', href: 'foo.ico' }], + ['link', { rel: 'stylesheet', href: 'foo.css' }], + ['link', { rel: 'canonical', href: 'foo' }], + // 2 + ['style', { type: 'text/css' }, 'body { color: red; }'], + ['style', { type: 'text/css' }, 'body { color: red; }'], + ['style', { type: 'text/css' }, 'body { color: white; }'], + ] - const dedupedHead = [ - // 1 - ['title', {}, 'foo'], - // 1 - ['base', { href: 'foo' }], - // 2 - ['meta', { name: 'foo', content: 'foo' }], - ['meta', { name: 'bar', content: 'bar' }], - // 2 - ['template', { id: 'foo' }, 'foo'], - ['template', { id: 'bar' }, 'bar'], - // 3 - ['script', { src: 'foo' }], - ['script', { src: 'bar' }], - ['script', { src: 'bar', async: true }], - // 2 - ['noscript', {}, 'foo'], - ['noscript', {}, 'bar'], - // 3 - ['link', { rel: 'icon', href: 'foo.ico' }], - ['link', { rel: 'stylesheet', href: 'foo.css' }], - ['link', { rel: 'canonical', href: 'foo' }], - // 2 - ['style', { type: 'text/css' }, 'body { color: red; }'], - ['style', { type: 'text/css' }, 'body { color: white; }'], - ] + const dedupedHead = [ + // 1 + ['title', {}, 'foo'], + // 1 + ['base', { href: 'foo' }], + // 2 + ['meta', { name: 'foo', content: 'foo' }], + ['meta', { name: 'bar', content: 'bar' }], + // 2 + ['template', { id: 'foo' }, 'foo'], + ['template', { id: 'bar' }, 'bar'], + // 3 + ['script', { src: 'foo' }], + ['script', { src: 'bar' }], + ['script', { src: 'bar', async: true }], + // 2 + ['noscript', {}, 'foo'], + ['noscript', {}, 'bar'], + // 3 + ['link', { rel: 'icon', href: 'foo.ico' }], + ['link', { rel: 'stylesheet', href: 'foo.css' }], + ['link', { rel: 'canonical', href: 'foo' }], + // 2 + ['style', { type: 'text/css' }, 'body { color: red; }'], + ['style', { type: 'text/css' }, 'body { color: white; }'], + ] - expect(dedupeHead(head)).toEqual(dedupedHead) - }) + expect(dedupeHead(head)).toEqual(dedupedHead) }) diff --git a/packages/shared/tests/ensureEndingSlash.spec.ts b/packages/shared/tests/ensureEndingSlash.spec.ts index cdd37f4c2c..a5913f34cf 100644 --- a/packages/shared/tests/ensureEndingSlash.spec.ts +++ b/packages/shared/tests/ensureEndingSlash.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { ensureEndingSlash } from '../src/index.js' const testCases = [ @@ -10,12 +10,8 @@ const testCases = [ ['/foo/bar.html', '/foo/bar.html'], ] -describe('shared > ensureEndingSlash', () => { - describe('should ensure ending slash for urls', () => { - testCases.forEach(([source, expected]) => { - it(source, () => { - expect(ensureEndingSlash(source)).toBe(expected) - }) - }) +testCases.forEach(([source, expected]) => { + it(source, () => { + expect(ensureEndingSlash(source)).toBe(expected) }) }) diff --git a/packages/shared/tests/ensureLeadingSlash.spec.ts b/packages/shared/tests/ensureLeadingSlash.spec.ts index b9487b96bc..43230a7463 100644 --- a/packages/shared/tests/ensureLeadingSlash.spec.ts +++ b/packages/shared/tests/ensureLeadingSlash.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { ensureLeadingSlash } from '../src/index.js' const testCases = [ @@ -10,12 +10,8 @@ const testCases = [ ['/foo/bar.html', '/foo/bar.html'], ] -describe('shared > ensureLeadingSlash', () => { - describe('should ensure leading slash for urls', () => { - testCases.forEach(([source, expected]) => { - it(source, () => { - expect(ensureLeadingSlash(source)).toBe(expected) - }) - }) +testCases.forEach(([source, expected]) => { + it(source, () => { + expect(ensureLeadingSlash(source)).toBe(expected) }) }) diff --git a/packages/shared/tests/formatDateString.spec.ts b/packages/shared/tests/formatDateString.spec.ts index 60b312c161..907e6786b1 100644 --- a/packages/shared/tests/formatDateString.spec.ts +++ b/packages/shared/tests/formatDateString.spec.ts @@ -17,20 +17,18 @@ const testCasesFallback = [ ['2020-01-001', '0000-00-00'], ] -describe('shared > formatDateString', () => { - describe('should format date string correctly', () => { - testCases.forEach(([source, expected]) => { - it(source, () => { - expect(formatDateString(source)).toBe(expected) - }) +describe('should format date string correctly', () => { + testCases.forEach(([source, expected]) => { + it(source, () => { + expect(formatDateString(source)).toBe(expected) }) }) +}) - describe('should fallback to default date string', () => { - testCasesFallback.forEach(([source, expected]) => { - it(source, () => { - expect(formatDateString(source, expected)).toBe(expected) - }) +describe('should fallback to default date string', () => { + testCasesFallback.forEach(([source, expected]) => { + it(source, () => { + expect(formatDateString(source, expected)).toBe(expected) }) }) }) diff --git a/packages/shared/tests/isLinkExternal.spec.ts b/packages/shared/tests/isLinkExternal.spec.ts index 96291a4b3d..27341d51d9 100644 --- a/packages/shared/tests/isLinkExternal.spec.ts +++ b/packages/shared/tests/isLinkExternal.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { isLinkExternal } from '../src/index.js' const testCases: [ @@ -62,15 +62,11 @@ const testCases: [ [['./foo/bar/baz.html', '/foo/'], false], ] -describe('shared > isLinkExternal', () => { - describe('should determine external link correctly', () => { - testCases.forEach(([[link, base = '/'], expected]) => { - it(`link: ${link}, base: ${base}`, () => { - expect(isLinkExternal(link, base)).toBe(expected) - expect(isLinkExternal(`${link}#foobar`, base)).toBe(expected) - expect(isLinkExternal(`${link}?foo=bar`, base)).toBe(expected) - expect(isLinkExternal(`${link}?foo=bar#foobar`, base)).toBe(expected) - }) - }) +testCases.forEach(([[link, base], expected]) => { + it(`link: ${link}, base: ${base}`, () => { + expect(isLinkExternal(link, base)).toBe(expected) + expect(isLinkExternal(`${link}#foobar`, base)).toBe(expected) + expect(isLinkExternal(`${link}?foo=bar`, base)).toBe(expected) + expect(isLinkExternal(`${link}?foo=bar#foobar`, base)).toBe(expected) }) }) diff --git a/packages/shared/tests/isLinkHttp.spec.ts b/packages/shared/tests/isLinkHttp.spec.ts index 021d58edf4..af54449acd 100644 --- a/packages/shared/tests/isLinkHttp.spec.ts +++ b/packages/shared/tests/isLinkHttp.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { isLinkHttp } from '../src/index.js' const testCases: [string, ReturnType][] = [ @@ -9,12 +9,8 @@ const testCases: [string, ReturnType][] = [ ['/foo/bar', false], ] -describe('shared > isLinkHttp', () => { - describe('should determine http link correctly', () => { - testCases.forEach(([source, expected]) => { - it(`link: ${source}`, () => { - expect(isLinkHttp(source)).toBe(expected) - }) - }) +testCases.forEach(([source, expected]) => { + it(`link: ${source}`, () => { + expect(isLinkHttp(source)).toBe(expected) }) }) diff --git a/packages/shared/tests/isLinkWithProtocol.spec.ts b/packages/shared/tests/isLinkWithProtocol.spec.ts index cacc8b318f..da2abf977a 100644 --- a/packages/shared/tests/isLinkWithProtocol.spec.ts +++ b/packages/shared/tests/isLinkWithProtocol.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { isLinkWithProtocol } from '../src/index.js' const testCases: [string, ReturnType][] = [ @@ -14,12 +14,8 @@ const testCases: [string, ReturnType][] = [ ['//foobar.com', false], ] -describe('shared > isLinkWithProtocol', () => { - describe('should determine link with protocol correctly', () => { - testCases.forEach(([source, expected]) => { - it(`link: ${source}`, () => { - expect(isLinkWithProtocol(source)).toBe(expected) - }) - }) +testCases.forEach(([source, expected]) => { + it(`link: ${source}`, () => { + expect(isLinkWithProtocol(source)).toBe(expected) }) }) diff --git a/packages/shared/tests/isPlainObject.spec.ts b/packages/shared/tests/isPlainObject.spec.ts index 393e9aae50..3b75e37ef3 100644 --- a/packages/shared/tests/isPlainObject.spec.ts +++ b/packages/shared/tests/isPlainObject.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { isPlainObject } from '../src/index.js' const testCases: [unknown, boolean][] = [ @@ -14,10 +14,8 @@ const testCases: [unknown, boolean][] = [ [Object.create(null), true], ] -describe('shared > isPlainObject', () => { - it('should determine plain object correctly', () => { - testCases.forEach(([source, expected]) => { - expect(isPlainObject(source)).toBe(expected) - }) +it('should determine plain object correctly', () => { + testCases.forEach(([source, expected]) => { + expect(isPlainObject(source)).toBe(expected) }) }) diff --git a/packages/shared/tests/removeEndingSlash.spec.ts b/packages/shared/tests/removeEndingSlash.spec.ts index e38eae38ac..45dfb1b52d 100644 --- a/packages/shared/tests/removeEndingSlash.spec.ts +++ b/packages/shared/tests/removeEndingSlash.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { removeEndingSlash } from '../src/index.js' const testCases = [ @@ -10,12 +10,8 @@ const testCases = [ ['/foo/bar.html', '/foo/bar.html'], ] -describe('shared > removeEndingSlash', () => { - describe('should remove ending slash', () => { - testCases.forEach(([source, expected]) => { - it(source, () => { - expect(removeEndingSlash(source)).toBe(expected) - }) - }) +testCases.forEach(([source, expected]) => { + it(source, () => { + expect(removeEndingSlash(source)).toBe(expected) }) }) diff --git a/packages/shared/tests/removeLeadingSlash.spec.ts b/packages/shared/tests/removeLeadingSlash.spec.ts index 9a7067fac4..5ecb891742 100644 --- a/packages/shared/tests/removeLeadingSlash.spec.ts +++ b/packages/shared/tests/removeLeadingSlash.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { removeLeadingSlash } from '../src/index.js' const testCases = [ @@ -10,12 +10,8 @@ const testCases = [ ['/foo/bar.html', 'foo/bar.html'], ] -describe('shared > removeLeadingSlash', () => { - describe('should remove ending slash', () => { - testCases.forEach(([source, expected]) => { - it(source, () => { - expect(removeLeadingSlash(source)).toBe(expected) - }) - }) +testCases.forEach(([source, expected]) => { + it(source, () => { + expect(removeLeadingSlash(source)).toBe(expected) }) }) diff --git a/packages/shared/tests/resolveHeadIdentifier.spec.ts b/packages/shared/tests/resolveHeadIdentifier.spec.ts index db0a18916b..569237147b 100644 --- a/packages/shared/tests/resolveHeadIdentifier.spec.ts +++ b/packages/shared/tests/resolveHeadIdentifier.spec.ts @@ -1,152 +1,164 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { resolveHeadIdentifier } from '../src/index.js' import type { HeadConfig } from '../src/index.js' -describe('shared > resolveHeadIdentifier', () => { - it('should resolve same identifiers of ', () => { - const titleFoo = resolveHeadIdentifier(['title', {}, 'title foo']) - const titleBar = resolveHeadIdentifier(['title', {}, 'title bar']) - expect(titleFoo).toBe(titleBar) - }) +it('should resolve same identifiers of <title>', () => { + const titleFoo = resolveHeadIdentifier(['title', {}, 'title foo']) + const titleBar = resolveHeadIdentifier(['title', {}, 'title bar']) + expect(titleFoo).toBe(titleBar) +}) - it('should resolve same identifiers of <base>', () => { - const baseFoo = resolveHeadIdentifier([ - 'base', - { href: 'https://vuepress.vuejs.org' }, - ]) - const baseBar = resolveHeadIdentifier([ - 'base', - { href: 'https://vuepress.github.io' }, - ]) - expect(baseFoo).toBe(baseBar) - }) +it('should resolve same identifiers of <base>', () => { + const baseFoo = resolveHeadIdentifier([ + 'base', + { href: 'https://vuepress.vuejs.org' }, + ]) + const baseBar = resolveHeadIdentifier([ + 'base', + { href: 'https://vuepress.github.io' }, + ]) + expect(baseFoo).toBe(baseBar) +}) - it('should resolve same identifiers of <meta> with same name', () => { - const metaFooBar = resolveHeadIdentifier([ - 'meta', - { name: 'foo', content: 'bar' }, - ]) - const metaFooBaz = resolveHeadIdentifier([ - 'meta', - { name: 'foo', content: 'baz' }, - ]) - const metaBarBar = resolveHeadIdentifier([ - 'meta', - { name: 'bar', content: 'bar' }, - ]) - expect(metaFooBar).toBe(metaFooBaz) - expect(metaFooBar).not.toBe(metaBarBar) - expect(metaFooBaz).not.toBe(metaBarBar) - }) +it('should resolve same identifiers of <meta> with same name', () => { + const metaFooBar = resolveHeadIdentifier([ + 'meta', + { name: 'foo', content: 'bar' }, + ]) + const metaFooBaz = resolveHeadIdentifier([ + 'meta', + { name: 'foo', content: 'baz' }, + ]) + const metaBarBar = resolveHeadIdentifier([ + 'meta', + { name: 'bar', content: 'bar' }, + ]) + expect(metaFooBar).toBe(metaFooBaz) + expect(metaFooBar).not.toBe(metaBarBar) + expect(metaFooBaz).not.toBe(metaBarBar) +}) - it('should resolve same identifiers of <template> with same id', () => { - const templateFooBar = resolveHeadIdentifier([ - 'template', - { id: 'foo' }, - 'bar', - ]) - const templateFooBaz = resolveHeadIdentifier([ - 'template', - { id: 'foo' }, - 'baz', - ]) - const templateBarBar = resolveHeadIdentifier([ - 'template', - { id: 'bar' }, - 'bar', - ]) - expect(templateFooBar).toBe(templateFooBaz) - expect(templateFooBar).not.toBe(templateBarBar) - expect(templateFooBaz).not.toBe(templateBarBar) - }) +it('should resolve same identifiers of <template> with same id', () => { + const templateFooBar = resolveHeadIdentifier([ + 'template', + { id: 'foo' }, + 'bar', + ]) + const templateFooBaz = resolveHeadIdentifier([ + 'template', + { id: 'foo' }, + 'baz', + ]) + const templateBarBar = resolveHeadIdentifier([ + 'template', + { id: 'bar' }, + 'bar', + ]) + expect(templateFooBar).toBe(templateFooBaz) + expect(templateFooBar).not.toBe(templateBarBar) + expect(templateFooBaz).not.toBe(templateBarBar) +}) - it('should resolve same identifiers of same HeadConfig', () => { - const style1 = resolveHeadIdentifier([ - 'style', - { id: 'foo' }, - `body { color: red; }`, - ]) - const style2 = resolveHeadIdentifier([ - 'style', - { id: 'foo' }, - `body { color: red; }`, - ]) - const link1 = resolveHeadIdentifier([ - 'link', - { href: 'https://example.com', defer: '' }, - ]) - const link2 = resolveHeadIdentifier([ - 'link', - { href: 'https://example.com', defer: true }, - ]) - const link3 = resolveHeadIdentifier([ - 'link', - { defer: '', href: 'https://example.com' }, - ]) - const link4 = resolveHeadIdentifier([ - 'link', - { defer: true, href: 'https://example.com' }, - ]) - const link5 = resolveHeadIdentifier([ - 'link', - { href: 'https://example.com' }, - ]) - const link6 = resolveHeadIdentifier([ - 'link', - { href: 'https://example.com', defer: false }, - ]) - const link7 = resolveHeadIdentifier([ - 'link', - { defer: false, href: 'https://example.com' }, - ]) +it('should resolve same identifiers of same HeadConfig', () => { + const style1 = resolveHeadIdentifier([ + 'style', + { id: 'foo' }, + `body { color: red; }`, + ]) + const style2 = resolveHeadIdentifier([ + 'style', + { id: 'foo' }, + `body { color: red; }`, + ]) + const link1 = resolveHeadIdentifier([ + 'link', + { href: 'https://example.com', defer: '' }, + ]) + const link2 = resolveHeadIdentifier([ + 'link', + { href: 'https://example.com', defer: true }, + ]) + const link3 = resolveHeadIdentifier([ + 'link', + { defer: '', href: 'https://example.com' }, + ]) + const link4 = resolveHeadIdentifier([ + 'link', + { defer: true, href: 'https://example.com' }, + ]) + const link5 = resolveHeadIdentifier(['link', { href: 'https://example.com' }]) + const link6 = resolveHeadIdentifier([ + 'link', + { href: 'https://example.com', defer: false }, + ]) + const link7 = resolveHeadIdentifier([ + 'link', + { defer: false, href: 'https://example.com' }, + ]) - expect(style1).toBe(style2) - expect(link1).toBe(link2) - expect(link1).toBe(link3) - expect(link1).toBe(link4) - expect(link5).toBe(link6) - expect(link5).toBe(link7) - }) + expect(style1).toBe(style2) + expect(link1).toBe(link2) + expect(link1).toBe(link3) + expect(link1).toBe(link4) + expect(link5).toBe(link6) + expect(link5).toBe(link7) +}) + +it('should resolve identifiers correctly', () => { + const head: HeadConfig[] = [ + // 1 + ['title', {}, 'foo'], + ['title', {}, 'bar'], + // 1 + ['base', { href: 'foo' }], + ['base', { href: 'bar' }], + // 2 + ['meta', { name: 'foo', content: 'foo' }], + ['meta', { name: 'foo', content: 'bar' }], + ['meta', { name: 'bar', content: 'bar' }], + // 2 + ['template', { id: 'foo' }, 'foo'], + ['template', { id: 'foo' }, 'bar'], + ['template', { id: 'bar' }, 'bar'], + // 3 + ['script', { src: 'foo' }], + ['script', { src: 'foo' }], + ['script', { src: 'bar' }], + ['script', { src: 'bar', async: true }], + // 2 + ['noscript', {}, 'foo'], + ['noscript', {}, 'foo'], + ['noscript', {}, 'bar'], + // 3 + ['link', { rel: 'icon', href: 'foo.ico' }], + ['link', { rel: 'icon', href: 'foo.ico' }], + ['link', { rel: 'stylesheet', href: 'foo.css' }], + ['link', { rel: 'canonical', href: 'foo' }], + // 2 + ['style', { type: 'text/css' }, 'body { color: red; }'], + ['style', { type: 'text/css' }, 'body { color: red; }'], + ['style', { type: 'text/css' }, 'body { color: white; }'], + ] + + const identifiers = head.map((item) => resolveHeadIdentifier(item)) + const filtered = [...new Set(identifiers)] + + expect(filtered).toHaveLength(1 + 1 + 2 + 2 + 3 + 2 + 3 + 2) +}) - it('should resolve identifiers correctly', () => { - const head: HeadConfig[] = [ - // 1 - ['title', {}, 'foo'], - ['title', {}, 'bar'], - // 1 - ['base', { href: 'foo' }], - ['base', { href: 'bar' }], - // 2 - ['meta', { name: 'foo', content: 'foo' }], - ['meta', { name: 'foo', content: 'bar' }], - ['meta', { name: 'bar', content: 'bar' }], - // 2 - ['template', { id: 'foo' }, 'foo'], - ['template', { id: 'foo' }, 'bar'], - ['template', { id: 'bar' }, 'bar'], - // 3 - ['script', { src: 'foo' }], - ['script', { src: 'foo' }], - ['script', { src: 'bar' }], - ['script', { src: 'bar', async: true }], - // 2 - ['noscript', {}, 'foo'], - ['noscript', {}, 'foo'], - ['noscript', {}, 'bar'], - // 3 - ['link', { rel: 'icon', href: 'foo.ico' }], - ['link', { rel: 'icon', href: 'foo.ico' }], - ['link', { rel: 'stylesheet', href: 'foo.css' }], - ['link', { rel: 'canonical', href: 'foo' }], - // 2 - ['style', { type: 'text/css' }, 'body { color: red; }'], - ['style', { type: 'text/css' }, 'body { color: red; }'], - ['style', { type: 'text/css' }, 'body { color: white; }'], - ] +it('should get null when tag is not allowed', () => { + const head: HeadConfig[] = [ + // @ts-expect-error: not allowed tag + ['html', {}, 'foo'], + // @ts-expect-error: not allowed tag + ['body', {}, 'bar'], + // @ts-expect-error: not allowed tag + ['div', {}, 'baz'], + // @ts-expect-error: not allowed tag + ['span', {}, 'qux'], + ] - const identifiers = head.map((item) => resolveHeadIdentifier(item)) - const filtered = [...new Set(identifiers)] + const identifiers = head.map((item) => resolveHeadIdentifier(item)) - expect(filtered).toHaveLength(1 + 1 + 2 + 2 + 3 + 2 + 3 + 2) - }) + expect(identifiers).toEqual([null, null, null, null]) }) diff --git a/packages/shared/tests/resolveLocalePath.spec.ts b/packages/shared/tests/resolveLocalePath.spec.ts index 238b51ce54..dc956e8607 100644 --- a/packages/shared/tests/resolveLocalePath.spec.ts +++ b/packages/shared/tests/resolveLocalePath.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { resolveLocalePath } from '../src/index.js' import type { LocaleConfig } from '../src/index.js' @@ -27,12 +27,8 @@ const testCases: [string, string][] = [ ['/foo-baz/foobar', '/foo-baz/'], ] -describe('shared > resolveLocalePath', () => { - describe('should resolve locale path correctly', () => { - testCases.forEach(([routePath, expected]) => { - it(routePath, () => { - expect(resolveLocalePath(locales, routePath)).toEqual(expected) - }) - }) +testCases.forEach(([routePath, expected]) => { + it(routePath, () => { + expect(resolveLocalePath(locales, routePath)).toEqual(expected) }) }) diff --git a/packages/shared/tests/resolveRoutePathFromUrl.spec.ts b/packages/shared/tests/resolveRoutePathFromUrl.spec.ts index b9cd70faa5..e360c89a9a 100644 --- a/packages/shared/tests/resolveRoutePathFromUrl.spec.ts +++ b/packages/shared/tests/resolveRoutePathFromUrl.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { resolveRoutePathFromUrl } from '../src/index.js' const testCases: [ @@ -23,14 +23,10 @@ const testCases: [ [['foo/bar', '/base/'], 'foo/bar'], ] -describe('shared > resolveRoutePathFromUrl', () => { - describe('should resolve route path correctly', () => { - testCases.forEach(([source, expected]) => { - it(`url: ${source[0]}, base: ${ - source[1] || '/' - } => expected: ${expected}`, () => { - expect(resolveRoutePathFromUrl(...source)).toEqual(expected) - }) - }) +testCases.forEach(([source, expected]) => { + it(`url: ${source[0]}, base: ${ + source[1] || '/' + } => expected: ${expected}`, () => { + expect(resolveRoutePathFromUrl(...source)).toEqual(expected) }) })