mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
chore: migrate test cases from Chai to Jest
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
import { last } from '../util'
|
||||
|
||||
function domResolve (root: string, path: string) {
|
||||
const base = document.createElement('base')
|
||||
base.href = root
|
||||
|
||||
const head = document.getElementsByTagName('head')[0]
|
||||
head.insertBefore(base, head.firstChild)
|
||||
|
||||
const a = document.createElement('a')
|
||||
a.href = path
|
||||
const resolved = a.href
|
||||
head.removeChild(base)
|
||||
|
||||
return resolved
|
||||
}
|
||||
|
||||
export function resolve (root: string, filepath: string, ext: string) {
|
||||
if (root.length && last(root) !== '/') root += '/'
|
||||
const url = domResolve(root, filepath)
|
||||
return url.replace(/^(\w+:\/\/[^/]+)(\/[^?]+)/, (str, origin, path) => {
|
||||
const last = path.split('/').pop()
|
||||
if (/\.\w+$/.test(last)) return str
|
||||
return origin + path + ext
|
||||
})
|
||||
}
|
||||
|
||||
export async function readFile (url: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest()
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve(xhr.responseText as string)
|
||||
} else {
|
||||
reject(new Error(xhr.statusText))
|
||||
}
|
||||
}
|
||||
xhr.onerror = () => {
|
||||
reject(new Error('An error occurred whilst receiving the response.'))
|
||||
}
|
||||
xhr.open('GET', url)
|
||||
xhr.send()
|
||||
})
|
||||
}
|
||||
|
||||
export function readFileSync (url: string): string {
|
||||
const xhr = new XMLHttpRequest()
|
||||
xhr.open('GET', url, false)
|
||||
xhr.send()
|
||||
if (xhr.status < 200 || xhr.status >= 300) {
|
||||
throw new Error(xhr.statusText)
|
||||
}
|
||||
return xhr.responseText as string
|
||||
}
|
||||
|
||||
export async function exists (filepath: string) {
|
||||
return true
|
||||
}
|
||||
|
||||
export function existsSync (filepath: string) {
|
||||
return true
|
||||
}
|
||||
|
||||
export function dirname (filepath: string) {
|
||||
return domResolve(filepath, '.')
|
||||
}
|
||||
|
||||
export const sep = '/'
|
||||
@@ -0,0 +1,53 @@
|
||||
import * as fs from './fs-impl'
|
||||
import * as path from 'path'
|
||||
|
||||
describe('fs-impl', function () {
|
||||
describe('.resolve()', function () {
|
||||
it('should resolve based on root', async function () {
|
||||
const filepath = fs.resolve('/foo', 'bar.html', '.liquid')
|
||||
const expected = path.resolve('/foo/bar.html')
|
||||
return expect(filepath).toBe(expected)
|
||||
})
|
||||
it('should add extension if it has no extension', async function () {
|
||||
const filepath = fs.resolve('/foo', 'bar', '.liquid')
|
||||
const expected = path.resolve('/foo/bar.liquid')
|
||||
return expect(filepath).toBe(expected)
|
||||
})
|
||||
})
|
||||
describe('.existsSync', () => {
|
||||
it('should resolve as false if not exists', () => {
|
||||
expect(fs.existsSync('/foo/bar')).toBeFalsy()
|
||||
})
|
||||
it('should resolve as true if exists', () => {
|
||||
expect(fs.existsSync(__filename)).toBeTruthy()
|
||||
})
|
||||
})
|
||||
describe('.exists', () => {
|
||||
it('should resolve as false if not exists', async () => {
|
||||
const result = await fs.exists('/foo/bar')
|
||||
expect(result).toBeFalsy()
|
||||
})
|
||||
it('should resolve as true if exists', async () => {
|
||||
const result = await fs.exists(__filename)
|
||||
expect(result).toBeTruthy()
|
||||
})
|
||||
})
|
||||
describe('.readFileSync', function () {
|
||||
it('should throw when not exist', function () {
|
||||
return expect(() => fs.readFileSync('/foo/bar')).toThrow('ENOENT')
|
||||
})
|
||||
it('should read content if exists', function () {
|
||||
const content = fs.readFileSync(__filename)
|
||||
expect(content).toContain('should read content if exists')
|
||||
})
|
||||
})
|
||||
describe('.readFile', function () {
|
||||
it('should throw when not exist', function () {
|
||||
return expect(fs.readFile('/foo/bar')).rejects.toHaveProperty('message', expect.stringMatching('ENOENT'))
|
||||
})
|
||||
it('should read content if exists', async function () {
|
||||
const content = await fs.readFile(__filename)
|
||||
expect(content).toContain('should read content if exists')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,31 @@
|
||||
import * as fs from './fs-impl'
|
||||
import { Loader } from './loader'
|
||||
|
||||
describe('fs/loader', function () {
|
||||
describe('.candidates()', function () {
|
||||
it('should resolve relatively', async function () {
|
||||
const loader = new Loader({ relativeReference: true, fs, extname: '' } as any)
|
||||
const candidates = [...loader.candidates('./foo/bar', ['/root', '/root/foo'], '/root/current', true)]
|
||||
expect(candidates).toContain('/root/foo/bar')
|
||||
})
|
||||
it('should not include out of root candidates', async function () {
|
||||
const loader = new Loader({ relativeReference: true, fs, extname: '' } as any)
|
||||
const candidates = [...loader.candidates('../foo/bar', ['/root'], '/root/current', true)]
|
||||
expect(candidates).toHaveLength(0)
|
||||
})
|
||||
it('should treat root as a terminated path', async function () {
|
||||
const loader = new Loader({ relativeReference: true, fs, extname: '' } as any)
|
||||
const candidates = [...loader.candidates('../root-dir/bar', ['/root'], '/root/current', true)]
|
||||
expect(candidates).toHaveLength(0)
|
||||
})
|
||||
it('should default `.contains()` to () => true', async function () {
|
||||
const customFs = {
|
||||
...fs,
|
||||
contains: undefined
|
||||
}
|
||||
const loader = new Loader({ relativeReference: true, fs: customFs, extname: '' } as any)
|
||||
const candidates = [...loader.candidates('../foo/bar', ['/root'], '/root/current', true)]
|
||||
expect(candidates).toContain('/foo/bar')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,10 +0,0 @@
|
||||
import { createRequire } from 'module'
|
||||
|
||||
export function requireResolve (file) {
|
||||
/**
|
||||
* createRequire() can throw,
|
||||
* when import.meta.url not begin with "file://".
|
||||
*/
|
||||
const require = createRequire(import.meta.url)
|
||||
return require.resolve(file)
|
||||
}
|
||||
Vendored
-1
@@ -1 +0,0 @@
|
||||
export declare function requireResolve(file: string): string;
|
||||
Reference in New Issue
Block a user