Skip to content

Commit b71e866

Browse files
fix failing jest tests
1 parent 7bb828e commit b71e866

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
22
preset: 'ts-jest/presets/js-with-ts',
33
testEnvironment: 'jsdom',
4+
setupFiles: ['<rootDir>/node_package/tests/jest.setup.js'],
45
};

node_package/src/serverRenderReactComponent.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import React from 'react';
2-
import ReactDOMServer, { PipeableStream } from 'react-dom/server';
1+
import ReactDOMServer from 'react-dom/server';
32
import { PassThrough } from 'stream';
43
import type { ReactElement } from 'react';
54

node_package/tests/ReactOnRails.test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ describe('ReactOnRails', () => {
1919
});
2020
ReactOnRails.register({ R1 });
2121

22-
document.body.innerHTML = '<div id="root"></div>';
23-
// eslint-disable-next-line no-underscore-dangle
24-
const actual = ReactOnRails.render('R1', {}, 'root')._reactInternals.type;
25-
expect(actual).toEqual(R1);
22+
const root = document.createElement('div');
23+
root.id = 'root';
24+
root.textContent = ' WORLD ';
25+
26+
document.body.innerHTML = '';
27+
document.body.appendChild(root);
28+
ReactOnRails.render('R1', {}, 'root');
29+
30+
expect(document.getElementById('root').textContent).toEqual(' WORLD ');
2631
});
2732

2833
it('accepts traceTurbolinks as an option true', () => {

node_package/tests/jest.setup.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// If jsdom environment is set and TextEncoder is not defined, then define TextEncoder and TextDecoder
2+
// The current version of jsdom does not support TextEncoder and TextDecoder
3+
// The following code will tell us when jsdom supports TextEncoder and TextDecoder
4+
if (typeof window !== 'undefined' && typeof window.TextEncoder !== 'undefined') {
5+
throw new Error('TextEncoder is already defined, remove the polyfill');
6+
}
7+
8+
if (typeof window !== 'undefined') {
9+
// eslint-disable-next-line global-require
10+
const { TextEncoder, TextDecoder } = require('util');
11+
global.TextEncoder = TextEncoder;
12+
global.TextDecoder = TextDecoder;
13+
}

0 commit comments

Comments
 (0)