Skip to content

Commit

Permalink
test(shared): improve unit tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Dec 29, 2023
1 parent eefa59d commit 3619e0e
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 299 deletions.
128 changes: 63 additions & 65 deletions packages/shared/tests/dedupeHead.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
12 changes: 4 additions & 8 deletions packages/shared/tests/ensureEndingSlash.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { expect, it } from 'vitest'
import { ensureEndingSlash } from '../src/index.js'

const testCases = [
Expand All @@ -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)
})
})
12 changes: 4 additions & 8 deletions packages/shared/tests/ensureLeadingSlash.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { expect, it } from 'vitest'
import { ensureLeadingSlash } from '../src/index.js'

const testCases = [
Expand All @@ -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)
})
})
20 changes: 9 additions & 11 deletions packages/shared/tests/formatDateString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
18 changes: 7 additions & 11 deletions packages/shared/tests/isLinkExternal.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { expect, it } from 'vitest'
import { isLinkExternal } from '../src/index.js'

const testCases: [
Expand Down Expand Up @@ -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)
})
})
12 changes: 4 additions & 8 deletions packages/shared/tests/isLinkHttp.spec.ts
Original file line number Diff line number Diff line change
@@ -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<typeof isLinkHttp>][] = [
Expand All @@ -9,12 +9,8 @@ const testCases: [string, ReturnType<typeof isLinkHttp>][] = [
['/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)
})
})
12 changes: 4 additions & 8 deletions packages/shared/tests/isLinkWithProtocol.spec.ts
Original file line number Diff line number Diff line change
@@ -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<typeof isLinkWithProtocol>][] = [
Expand All @@ -14,12 +14,8 @@ const testCases: [string, ReturnType<typeof isLinkWithProtocol>][] = [
['//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)
})
})
10 changes: 4 additions & 6 deletions packages/shared/tests/isPlainObject.spec.ts
Original file line number Diff line number Diff line change
@@ -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][] = [
Expand All @@ -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)
})
})
12 changes: 4 additions & 8 deletions packages/shared/tests/removeEndingSlash.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { expect, it } from 'vitest'
import { removeEndingSlash } from '../src/index.js'

const testCases = [
Expand All @@ -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)
})
})
12 changes: 4 additions & 8 deletions packages/shared/tests/removeLeadingSlash.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { expect, it } from 'vitest'
import { removeLeadingSlash } from '../src/index.js'

const testCases = [
Expand All @@ -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)
})
})
Loading

0 comments on commit 3619e0e

Please sign in to comment.