Skip to content

Commit

Permalink
Add tests for block/remove processors (and make fixes!)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Apr 5, 2019
1 parent d8e291b commit 690373d
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 8 deletions.
2 changes: 1 addition & 1 deletion source/standard/processors/RemoveAttributeProcessor.js
Expand Up @@ -61,6 +61,6 @@ export default class RemoveAttributeProcessor extends AttributeProcessor {
element.parentElement.removeChild(element);
}

return false;
return true;
}
}
6 changes: 2 additions & 4 deletions source/utilities/Fragments.js
Expand Up @@ -39,11 +39,9 @@ export async function extractFragment(fragmentInfo, context) {
let fragmentProcessorName = FragmentAttributeProcessor.NAME;

return $(`[${dialectPrefix}\\:${fragmentProcessorName}^="${fragmentName}"]`, template) ||
$(`[data-${dialectPrefix}-${fragmentProcessorName}^="${fragmentName}"`, template);
}
else {
console.log('No template resolver configured');
$(`[data-${dialectPrefix}-${fragmentProcessorName}^="${fragmentName}"]`, template);
}

console.log('No template resolver configured');
return null;
}
2 changes: 1 addition & 1 deletion source/utilities/Functions.js
Expand Up @@ -26,7 +26,7 @@ export function promisify(func) {
return new Promise((resolve, reject) => {
func(...arguments, (error, result) => {
if (error) {
reject(new Error(error));
reject(new Error(result));
}
else {
resolve(result);
Expand Down
49 changes: 49 additions & 0 deletions test/standard/processors/BlockElementProcessor.js
@@ -0,0 +1,49 @@
/*
* Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import BlockElementProcessor from '../../../source/standard/processors/BlockElementProcessor.js';

import h from 'hyperscript';
import hh from 'hyperscript-helpers';

const {div, p} = hh(h);

/**
* Tests for the block element processor.
*/
describe('standard/processors/BlockElementProcessor', function() {

let processor;
let elementName;
beforeAll(function() {
processor = new BlockElementProcessor('test');
elementName = `${processor.prefix}:${processor.name}`;
});

test('Scoped values copied down to child elements ', function() {
let element = document.createElement(elementName);
element.__thymeleafLocalVariables = {};
let paragraph = p('Hello!');
element.appendChild(paragraph);
div([
element
]);

processor.process(element, {});

expect(paragraph).toHaveProperty('__thymeleafLocalVariables', element.__thymeleafLocalVariables);
});
});
2 changes: 1 addition & 1 deletion test/template-expected.html
Expand Up @@ -32,7 +32,7 @@
</div>
<p>This is some text from an external fragment</p>

Surrounding element will disappear
Surrounding element removed via synthetic block.

</body>

Expand Down
6 changes: 5 additions & 1 deletion test/template.html
Expand Up @@ -26,8 +26,12 @@
Fragment replaces this
</div>

<div thjs:remove="all">
This will all be removed.
</div>

<thjs:block>
Surrounding element will disappear
Surrounding element removed via synthetic block.
</thjs:block>
</body>

Expand Down
49 changes: 49 additions & 0 deletions test/utilities/Dom.js
@@ -0,0 +1,49 @@
/*
* Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
createThymeleafAttributeValue,
getThymeleafAttributeValue
} from '../../source/utilities/Dom.js';

import h from 'hyperscript';
import hh from 'hyperscript-helpers';

const {div} = hh(h);

/**
* Tests for the DOM utilities.
*/
describe('utilities/Dom', function() {

describe('#getThymeleafAttributeValue', function() {
const prefix = 'thjs';
const name = 'test';
const value = 'hello';

test('Returns the XML attribute value', function() {
let element = createThymeleafAttributeValue(div(), `${prefix}:${name}`, value);
let result = getThymeleafAttributeValue(element, prefix, name);
expect(result).toBe(value);
});

test('Returns the data- attribute value', function() {
let element = createThymeleafAttributeValue(div(), `data-${prefix}-${name}`, value);
let result = getThymeleafAttributeValue(element, prefix, name);
expect(result).toBe(value);
});
});
});
69 changes: 69 additions & 0 deletions test/utilities/Fragments.js
@@ -0,0 +1,69 @@
/*
* Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import FragmentAttributeProcessor from '../../source/standard/processors/FragmentAttributeProcessor.js';
import StandardDialect from '../../source/standard/StandardDialect.js';
import {extractFragment} from '../../source/utilities/Fragments.js';

/**
* Tests for the Fragments utility.
*/
describe('utilities/Fragments', function() {

describe('#extractFragment', function() {
const dialects = [
{
name: StandardDialect.NAME,
prefix: StandardDialect.DEFAULT_PREFIX
}
];
const fragmentInfo = {
fragmentName: 'test-fragment',
templateName: 'test-template'
};

let fragmentAttributeName;
const templateResolver = () => `
<body>
<div ${fragmentAttributeName}="${fragmentInfo.fragmentName}">Hi!</div>
<p ${fragmentAttributeName}="wrong-fragment">Hi!</p>
</body>
`;
const context = {
dialects,
templateResolver
};

test('Locates fragments by XML name', async function() {
fragmentAttributeName = `${StandardDialect.DEFAULT_PREFIX}:${FragmentAttributeProcessor.NAME}`;
let result = await extractFragment(fragmentInfo, context);
expect(result.tagName).toBe('DIV');
expect(result.textContent).toBe('Hi!');
});

test('Locates fragments by data- attribute name', async function() {
fragmentAttributeName = `data-${StandardDialect.DEFAULT_PREFIX}-${FragmentAttributeProcessor.NAME}`;
let result = await extractFragment(fragmentInfo, context);
expect(result.tagName).toBe('DIV');
expect(result.textContent).toBe('Hi!');
});

test('No template resolver returns `null`', async function() {
let result = await extractFragment(null, {});
expect(result).toBe(null);
});
});
});
40 changes: 40 additions & 0 deletions test/utilities/Functions.js
@@ -0,0 +1,40 @@
/*
* Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {promisify} from '../../source/utilities/Functions.js';

/**
* Tests for the function utilities.
*/
describe('utilities/Functions', function() {

describe('#promisify', function() {

test('Promise maps success to the `then` handler', async function() {
let promisifiedFunc = promisify((callback) => {
callback(false, 'Hello!');
});
await expect(promisifiedFunc()).resolves.toBe('Hello!');
});

test('Promise maps failure to the `catch` handler', async function() {
let promisifiedFunc = promisify((callback) => {
callback(true, 'Goodbye!');
});
await expect(promisifiedFunc()).rejects.toThrow('Goodbye!');
});
});
});

0 comments on commit 690373d

Please sign in to comment.