Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests #75

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dist
dist-ssr
*.local
_*
!__tests__

**/worker/*
pnpm-lock.yaml
15 changes: 15 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Config } from '@jest/types'

const config: Config.InitialOptions = {
testMatch: ['**/*.spec.[jt]s?(x)'],
transform: {
'^.+\\.tsx?$': [
'esbuild-jest',
{
sourcemap: true,
},
],
},
}

export default config
15 changes: 15 additions & 0 deletions src/__tests__/utils/html.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { renderPreloadLinks } from '../../utils/html'

describe('renderPreloadLinks', () => {
test('should render .js and .css files', () => {
const links = renderPreloadLinks(['foo.js', 'bar.css'])
expect(links).toMatchInlineSnapshot(
`"<link rel=\\"modulepreload\\" crossorigin href=\\"foo.js\\"><link rel=\\"stylesheet\\" href=\\"bar.css\\">"`
)
})

test('should return empty string if not .js or .css file', () => {
const links = renderPreloadLinks(['foo.svg'])
expect(links.length).toEqual(0)
})
})
85 changes: 85 additions & 0 deletions src/__tests__/utils/route.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
withPrefix,
withoutPrefix,
withSuffix,
withoutSuffix,
createUrl,
joinPaths,
getFullPath,
} from '../../utils/route'

describe('withPrefix', () => {
test('should return original string', () => {
expect(withPrefix('/foo/bar', '/foo')).toEqual('/foo/bar')
})

test('should return prefixed string', () => {
expect(withPrefix('/bar/baz', '/foo')).toEqual('/foo/bar/baz')
})
})

describe('withoutPrefix', () => {
test('should return original string', () => {
expect(withoutPrefix('/foo/bar', '/baz')).toEqual('/foo/bar')
})

test('should return string without prefix', () => {
expect(withoutPrefix('/foo/bar', '/foo')).toEqual('/bar')
})
})

describe('withSuffix', () => {
test('should return original string', () => {
expect(withSuffix('/foo/bar', '/bar')).toEqual('/foo/bar')
})

test('should return string with suffix', () => {
expect(withSuffix('/foo/bar', '/baz')).toEqual('/foo/bar/baz')
})
})

describe('withoutSuffix', () => {
test('should return string with suffix', () => {
expect(withoutSuffix('/foo/bar', '/baz')).toEqual('/foo/bar/baz')
})

test('should return string without suffix', () => {
expect(withoutSuffix('/foo/bar', '/bar')).toEqual('/foo')
})
})

describe('createUrl', () => {
const url = 'https://www.foo.com'

test('should return URL instance', () => {
expect(createUrl(url)).toBeInstanceOf(URL)
})

test('should return original URL', () => {
expect(createUrl(url)).toEqual(new URL(url))
expect(createUrl(new URL(url))).toEqual(new URL(url))
})

test('should return example URL if passed URL is not valid', () => {
expect(createUrl('invalid-url')).toEqual(new URL(`http://e.g/invalid-url`))
})
})

describe('joinPaths', () => {
test('should join paths to string', () => {
const path = joinPaths('foo', '//bar', '/baz', '/boo')
expect(path).toEqual('foo/bar/baz/boo')
})
})

describe('getFullPath', () => {
test('should return full path', () => {
const path = getFullPath('https://foo.com/some/path')
expect(path).toEqual('/some/path')
})

test('should return path without base', () => {
const path = getFullPath('https://foo.com/some/path', '/some')
expect(path).toEqual('/path')
})
})
17 changes: 17 additions & 0 deletions src/__tests__/utils/state.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { deserializeState, serializeState } from '../../utils/state'

describe('state:', () => {
const state = { foo: 'baz', bar: 5, boo: [], bee: {} }
const serializedState = serializeState(state)

test('serializeState', () => {
expect(serializedState).toMatchInlineSnapshot(
`"\\"{\\\\\\"foo\\\\\\":\\\\\\"baz\\\\\\",\\\\\\"bar\\\\\\":5,\\\\\\"boo\\\\\\":[],\\\\\\"bee\\\\\\":{}}\\""`
)
})

test('deserializeState', () => {
// @ TODO
//expect(deserializeState(serializedState)).toEqual(state)
})
})
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1018,9 +1018,9 @@ [email protected]:
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=

electron-to-chromium@^1.3.793:
version "1.3.793"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.793.tgz#c10dff5f3126238004de344db458f1da3641d554"
integrity sha512-l9NrGV6Mr4ov5mayYPvIWcwklNw5ROmy6rllzz9dCACw9nKE5y+s5uQk+CBJMetxrWZ6QJFsvEfG6WDcH2IGUg==
version "1.3.795"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.795.tgz#91f09b4c77f8dab562dd592ce929b009201c24ff"
integrity sha512-4TPxrLf9Fzsi4rVgTlDm+ubxoXm3/TN67/LGHx/a4UkVubKILa6L26O6eTnHewixG/knzU9L3lLmfL39eElwlQ==

emojis-list@^3.0.0:
version "3.0.0"
Expand Down