stream-tag
Version:
A tagged template literal utility for Node streams
96 lines (90 loc) • 3.07 kB
JavaScript
;
var _react = require("react");
var _server = require("react-dom/server");
var _streamToString = _interopRequireDefault(require("stream-to-string"));
var _index = _interopRequireDefault(require("../index"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
describe('streamTag', () => {
it('handles strings-only correctly', () => {
const stream = _index.default`test`;
return (0, _streamToString.default)(stream).then(string => {
expect(string).toBe('test');
});
});
it('handles empty templates correctly', () => {
const stream = _index.default``;
return (0, _streamToString.default)(stream).then(string => {
expect(string).toBe('');
});
});
it('handles falsy templates correctly', () => {
const stream = _index.default`${undefined}${null}${false}`;
return (0, _streamToString.default)(stream).then(string => {
expect(string).toBe('');
});
});
it('handles string interpolations correctly', () => {
const stream = _index.default`test${'test'}`;
return (0, _streamToString.default)(stream).then(string => {
expect(string).toBe('testtest');
});
});
it('handles number interpolations correctly', () => {
const stream = _index.default`${0}test${9}`;
return (0, _streamToString.default)(stream).then(string => {
expect(string).toBe('0test9');
});
});
it('handles promise interpolations correctly', () => {
const stream = _index.default`x${Promise.resolve('test')}x`;
return (0, _streamToString.default)(stream).then(string => {
expect(string).toBe('xtestx');
});
});
it('handles nested streams correctly', () => {
const stream = _index.default`x${_index.default`test`}x`;
return (0, _streamToString.default)(stream).then(string => {
expect(string).toBe('xtestx');
});
});
it('handles Buffers correctly', () => {
const stream = _index.default`x${Buffer.from('test')}x`;
return (0, _streamToString.default)(stream).then(string => {
expect(string).toBe('xtestx');
});
});
it('handles deferred interolations correctly', () => {
const stream = _index.default`
${() => 'test'}
${() => Buffer.from('test')}
${() => Promise.resolve('test')}
${() => _index.default`test`}
`;
const output = `
test
test
test
test
`;
return (0, _streamToString.default)(stream).then(string => {
expect(string).toBe(output);
});
});
describe('React SSR Integration', () => {
it('correctly interpolates renderToNodeStream', () => {
const tree = (0, _react.createElement)('div', {
className: 'test'
}, (0, _react.createElement)('h1', {}, 'Hello World!'));
const stream = _index.default`
<html>
<body>
${(0, _server.renderToNodeStream)(tree)}
</body>
</html>
`;
return (0, _streamToString.default)(stream).then(string => {
expect(string).toMatchSnapshot();
});
});
});
});