mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
chore: migrate test cases from Chai to Jest
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
import * as fs from './fs-impl-browser'
|
||||
import * as sinon from 'sinon'
|
||||
import { JSDOM } from 'jsdom'
|
||||
|
||||
describe('fs/browser', function () {
|
||||
if (+(process.version.match(/^v(\d+)/) as RegExpMatchArray)[1] < 8) {
|
||||
console.info('jsdom not supported, skipping template-browser...')
|
||||
return
|
||||
}
|
||||
beforeEach(function () {
|
||||
const dom = new JSDOM(``, {
|
||||
url: 'https://example.com/foo/bar/',
|
||||
contentType: 'text/html',
|
||||
includeNodeLocations: true
|
||||
});
|
||||
(global as any).document = dom.window.document
|
||||
})
|
||||
afterEach(function () {
|
||||
delete (global as any).document
|
||||
})
|
||||
describe('#resolve()', function () {
|
||||
it('should support relative root', function () {
|
||||
expect(fs.resolve('./views/', 'foo', '')).toBe('https://example.com/foo/bar/views/foo')
|
||||
})
|
||||
it('should treat root as directory', function () {
|
||||
expect(fs.resolve('./views', 'foo', '')).toBe('https://example.com/foo/bar/views/foo')
|
||||
})
|
||||
it('should support absolute root', function () {
|
||||
expect(fs.resolve('/views', 'foo', '')).toBe('https://example.com/views/foo')
|
||||
})
|
||||
it('should support empty root', function () {
|
||||
expect(fs.resolve('', 'page.html', '')).toBe('https://example.com/foo/bar/page.html')
|
||||
})
|
||||
it('should support full url as root', function () {
|
||||
expect(fs.resolve('https://example.com/views/', 'page.html', '')).toBe('https://example.com/views/page.html')
|
||||
})
|
||||
it('should add extname when absent', function () {
|
||||
expect(fs.resolve('https://example.com/views/', 'page', '.html')).toBe('https://example.com/views/page.html')
|
||||
})
|
||||
it('should add extname for urls have searchParams', function () {
|
||||
expect(fs.resolve('https://example.com/views/', 'page?foo=bar', '.html')).toBe('https://example.com/views/page.html?foo=bar')
|
||||
})
|
||||
it('should not add extname when full url is given', function () {
|
||||
expect(fs.resolve('https://example.com/views/', 'https://google.com/page.php', '.html')).toBe('https://google.com/page.php')
|
||||
})
|
||||
it('should not add extname when already have one', function () {
|
||||
expect(fs.resolve('https://example.com/views/', 'page.php', '.html')).toBe('https://example.com/views/page.php')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#dirname()', () => {
|
||||
it('should return dirname of file', async function () {
|
||||
const val = fs.dirname('https://example.com/views/foo/bar')
|
||||
expect(val).toBe('https://example.com/views/foo/')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#exists()', () => {
|
||||
it('should always return true', async function () {
|
||||
const val = await fs.exists('/foo/bar')
|
||||
expect(val).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#existsSync()', () => {
|
||||
it('should always return true', function () {
|
||||
expect(fs.existsSync('/foo/bar')).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#readFile()', () => {
|
||||
let server: sinon.SinonFakeServer
|
||||
beforeEach(() => {
|
||||
server = sinon.fakeServer.create()
|
||||
server.autoRespond = true
|
||||
server.respondWith('GET', 'https://example.com/views/hello.html',
|
||||
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}']);
|
||||
(global as any).XMLHttpRequest = sinon.useFakeXMLHttpRequest()
|
||||
})
|
||||
afterEach(() => {
|
||||
server.restore()
|
||||
delete (global as any).XMLHttpRequest
|
||||
})
|
||||
it('should get corresponding text', async function () {
|
||||
const html = await fs.readFile('https://example.com/views/hello.html')
|
||||
return expect(html).toBe('hello {{name}}')
|
||||
})
|
||||
it('should throw 404', () => {
|
||||
return expect(fs.readFile('https://example.com/not/exist.html'))
|
||||
.rejects.toHaveProperty('message', 'Not Found')
|
||||
})
|
||||
it('should throw error', function () {
|
||||
const result = expect(fs.readFile('https://example.com/views/hello.html'))
|
||||
.rejects.toHaveProperty('message', 'An error occurred whilst receiving the response.')
|
||||
server.requests[0].error()
|
||||
return result
|
||||
})
|
||||
})
|
||||
|
||||
describe('#readFileSync()', () => {
|
||||
let server: sinon.SinonFakeServer
|
||||
beforeEach(() => {
|
||||
server = sinon.fakeServer.create()
|
||||
server.autoRespond = true
|
||||
server.respondWith(
|
||||
'GET', 'https://example.com/views/hello.html',
|
||||
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}']
|
||||
);
|
||||
(global as any).XMLHttpRequest = sinon.useFakeXMLHttpRequest()
|
||||
})
|
||||
afterEach(() => {
|
||||
server.restore()
|
||||
delete (global as any).XMLHttpRequest
|
||||
})
|
||||
it('should get corresponding text', function () {
|
||||
const html = fs.readFileSync('https://example.com/views/hello.html')
|
||||
return expect(html).toBe('hello {{name}}')
|
||||
})
|
||||
it('should throw 404', () => {
|
||||
return expect(() => fs.readFileSync('https://example.com/not/exist.html'))
|
||||
.toThrow('Not Found')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,68 @@
|
||||
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,10 @@
|
||||
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
@@ -0,0 +1 @@
|
||||
export declare function requireResolve(file: string): string;
|
||||
@@ -0,0 +1,7 @@
|
||||
import { StreamedEmitter } from './streamed-emitter-browser'
|
||||
|
||||
describe('build/streamed-emitter-browser', () => {
|
||||
it('should throw when try to constructing', () => {
|
||||
expect(() => new StreamedEmitter()).toThrow(/streaming not supported/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Emitter } from '../emitters'
|
||||
|
||||
export class StreamedEmitter implements Emitter {
|
||||
public buffer = '';
|
||||
public stream: NodeJS.ReadableStream = null as any
|
||||
constructor () {
|
||||
throw new Error('streaming not supported in browser')
|
||||
}
|
||||
public write: (html: any) => void
|
||||
public error: (err: Error) => void
|
||||
public end: () => void
|
||||
}
|
||||
Reference in New Issue
Block a user