-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(html): correctly transforms html source when needed
- Loading branch information
Showing
3 changed files
with
29 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set 1`] = ` | ||
"module.exports=\`<div class=\\"html-test\\"> | ||
exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set: module 1`] = ` | ||
"<div class=\\"html-test\\"> | ||
<span class=\\"html-test__element\\">This is element</span> | ||
</div>\`;" | ||
<code>This is a backtilt \`</code> | ||
</div>" | ||
`; | ||
exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set 2`] = ` | ||
exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set: source 1`] = `"module.exports=\\"<div class=\\\\\\"html-test\\\\\\">\\\\n <span class=\\\\\\"html-test__element\\\\\\">This is element</span>\\\\n <code>This is a backtilt \`</code>\\\\n</div>\\";"`; | ||
exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set: untransformed 1`] = ` | ||
"<div class=\\"html-test\\"> | ||
<span class=\\"html-test__element\\">This is element</span> | ||
<code>This is a backtilt \`</code> | ||
</div>" | ||
`; |
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 |
---|---|---|
@@ -1,19 +1,30 @@ | ||
import { process } from '../../src/preprocessor'; | ||
|
||
const path = '/path/to/file.html'; | ||
const config = { globals: {} as any }; | ||
// wrap a transformed source so that we can fake a `require()` on it by calling the returned wrapper | ||
const wrap = (src: string) => | ||
new Function(`var module={}; ${src} return module.exports;`) as any; | ||
|
||
const source = `<div class="html-test"> | ||
<span class="html-test__element">This is element</span> | ||
<code>This is a backtilt \`</code> | ||
</div>`; | ||
const path = '/path/to/file.html'; | ||
const config = { | ||
globals: { | ||
__TRANSFORM_HTML__: true, | ||
}, | ||
}; | ||
|
||
describe('Html transforms', () => { | ||
it('transforms html if config.globals.__TRANSFORM_HTML__ is set', () => { | ||
expect(process(source, path, config)).toMatchSnapshot(); | ||
delete config.globals.__TRANSFORM_HTML__; | ||
expect(process(source, path, config)).toMatchSnapshot(); | ||
// get the untransformed version | ||
const untransformed = process(source, path, config); | ||
// ... then the one which should be transformed | ||
config.globals.__TRANSFORM_HTML__ = true; | ||
const transformed = process(source, path, config) as string; | ||
// ... finally the result of a `require('module-with-transformed-version')` | ||
const exported = wrap(transformed)(); | ||
|
||
expect(exported).toMatchSnapshot('module'); | ||
expect(transformed).toMatchSnapshot('source'); | ||
expect(untransformed).toMatchSnapshot('untransformed'); | ||
// requiring the transformed version should return the same string as the untransformed version | ||
expect(exported).toBe(untransformed); | ||
}); | ||
}); |