Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(insights): add hogan helper (#4253)
- Loading branch information
Showing
3 changed files
with
108 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import renderTemplate from '../renderTemplate'; | ||
import createHelpers from '../../createHelpers'; | ||
import { insights } from '../../../helpers'; | ||
|
||
describe('insights hogan helper', () => { | ||
const helpers = createHelpers({ numberLocale: 'en-US' }); | ||
it('should produce the same output as the insights function', () => { | ||
const output = renderTemplate({ | ||
templateKey: 'item', | ||
templates: { | ||
item: ` | ||
<button {{#helpers.insights}}{ | ||
"method": "clickedObjectIDsAfterSearch", | ||
"payload": {"eventName": "Add to cart"} | ||
}{{/helpers.insights}}>Add to cart</button> | ||
`, | ||
}, | ||
helpers, | ||
data: { | ||
objectID: 'xxx', | ||
}, | ||
}); | ||
|
||
const expected = `<button ${insights('clickedObjectIDsAfterSearch', { | ||
objectIDs: ['xxx'], | ||
eventName: 'Add to cart', | ||
})}>Add to cart</button> | ||
`; | ||
|
||
expect(output.trim()).toEqual(expected.trim()); | ||
}); | ||
it('should throw an error if the provided string cannot be JSON parsed', () => { | ||
expect(() => | ||
renderTemplate({ | ||
templateKey: 'item', | ||
templates: { | ||
item: ` | ||
<button {{#helpers.insights}}{ | ||
invalid json | ||
}{{/helpers.insights}}>Add to cart</button> | ||
`, | ||
}, | ||
helpers, | ||
data: { | ||
objectID: 'xxx', | ||
}, | ||
}) | ||
).toThrowErrorMatchingInlineSnapshot(` | ||
" | ||
The insights helper expects a JSON object of the format: | ||
{ \\"method\\": \\"method-name\\", \\"payload\\": { \\"eventName\\": \\"name of the event\\" } }" | ||
`); | ||
}); | ||
}); |
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