import path from 'path' import { PageKitHandlebars as Subject } from '../PageKitHandlebars' // NOTE: Tests are run from the repository root directory so we need to set the CWD const root = path.join(__dirname, '__fixtures__') const view = path.resolve(root, 'views/view.hbs') describe('dotcom-server-handlebars/src/PageKitHandlebars', () => { let instance beforeEach(() => { instance = new Subject({ rootDirectory: root, cache: true }) }) describe('.loadPartials()', () => { let result beforeEach(() => { result = instance.loadPartials() }) it('loads and compiles partial templates from disk', () => { const values = Object.values(result) expect(values.length).toBe(3) values.forEach((template) => { expect(template).toEqual(expect.any(Function)) }) }) it('caches each compiled partial template', () => { const keys = Object.keys(result) keys.forEach((key) => { const filename = path.join(root, `views/partials/${key}.hbs`) expect(instance.cache.has(filename)).toBe(true) }) }) }) describe('.loadTemplate()', () => { let result beforeEach(() => { result = instance.loadTemplate(view) }) it('loads and compiles a template from disk', () => { expect(result).toEqual(expect.any(Function)) }) it('caches the compiled template', () => { expect(instance.cache.has(view)).toBe(true) }) }) describe('.render()', () => { const templateContext = { title: 'Hello World', aside: 'Lorem ipsum' } it('can render a template from disk', () => { const result = instance.render(view, templateContext) expect(result).toContain('

Hello World

') expect(result).toContain('') expect(result).toMatch(/
.+<\/main>/s) }) it('can render a given template function', () => { const template = instance.loadTemplate(view) const result = instance.render(template, templateContext) expect(result).toContain('

Hello World

') expect(result).toContain('') expect(result).toMatch(/
.+<\/main>/s) }) }) describe('.renderView()', () => { it('can render a template and fire a callback with the result', async () => { const spy = jest.fn() const templateContext = { title: 'Hello World', aside: 'Lorem ipsum' } await instance.renderView(view, templateContext, spy) const callbackParameters = spy.mock.calls[0] const [callbackError, callbackSuccess] = callbackParameters expect(spy).toHaveBeenCalledTimes(1) expect(callbackError).toBeNull() expect(callbackSuccess).toContain('

Hello World

') expect(callbackSuccess).toContain('') expect(callbackSuccess).toContain('
') }) }) })