Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for block/remove processors (and make fixes!)
- Loading branch information
Showing
9 changed files
with
217 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!'); | ||
}); | ||
}); | ||
}); |