import { ViewHelper } from '../../lib'; function createDemoElement(): HTMLElement { var demoElement: HTMLDivElement = document.createElement('div'); var paragraph = document.createElement('p'); paragraph.appendChild(document.createTextNode('Hallo Welt {{demo.test}}')); demoElement.appendChild(paragraph); return demoElement; } function createDemoElementWithTemplate(): HTMLElement { var demoElement: HTMLDivElement = createDemoElement() as HTMLDivElement; var template = document.createElement("template"); template.innerHTML = "

{{demo.test}}

" demoElement.append(template); return demoElement; } describe('markElement', () => { const input = 'We'; test('mark Element in Text', () => { const element: HTMLElement = createDemoElement(); ViewHelper.markElement(element, input); expect(element.innerHTML).toMatch('We'); }); test('do nothing if input is empty', () => { const element: HTMLElement = createDemoElement(); ViewHelper.markElement(element, ''); expect(element.innerHTML).toBe(createDemoElement().innerHTML); }); }); describe('regexTemplate', () => { test('return expected template', () => { const expected = /{{([a-zA-Z0-9\.ß\[\]]+)(\|[a-zA-Z]+){0,1}}}/; expect(ViewHelper.regexTemplate).toStrictEqual(expected); }); }); describe('replacePlaceholders', () => { const object = { demo: { test: 'Blabla', }, }; test('should replace placeholder', () => { const element: HTMLElement = createDemoElement(); ViewHelper.replacePlaceholders(element, object); expect(element.innerHTML).toMatch(object.demo.test); }); test('should replace empty or unkown value', () => { const element: HTMLElement = createDemoElement(); element.innerHTML = element.innerHTML.replace('demo.test', 'demo.xxx'); ViewHelper.replacePlaceholders(element, object); expect(element.innerHTML).toMatch('

Hallo Welt

'); }); }); describe('getParameter', () => { const testKey = "testKey"; const testValue = "1"; beforeEach(function() { // @ts-ignore delete window.location; const url = new URL('http://localhost/demo'); url.searchParams.append(testKey, testValue); // @ts-ignore window.location = url; }); it('load Parameter', () => { const loadedKey = ViewHelper.getParameter(testKey); expect(loadedKey).toEqual(testValue); }); it('load non existing Parameter', () => { const loadedKey = ViewHelper.getParameter("unknownKey"); expect(loadedKey).toEqual(null); }); });