Skip to content

Commit 1013caf

Browse files
committed
fix tests
1 parent 0f368c1 commit 1013caf

File tree

4 files changed

+71
-29
lines changed

4 files changed

+71
-29
lines changed
Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,62 @@
11
import React from 'react';
22
import { expect } from '../../testing/chai';
3-
import { shallow } from '../../testing/enzyme';
3+
import { screen, render, waitFor, cleanup } from '@testing-library/react';
44

55
import type { ShellOutputEntry } from './shell-output-line';
6-
import { ShellOutputLine } from './shell-output-line';
76
import { ShellOutput } from './shell-output';
87

8+
function WrappedShellOutput(props: { output: ShellOutputEntry[] }) {
9+
return (
10+
<div style={{ height: '200px' }}>
11+
<ShellOutput
12+
output={props.output}
13+
__TEST_LIST_HEIGHT={200}
14+
setInnerContainerRef={() => {
15+
/** */
16+
}}
17+
/>
18+
</div>
19+
);
20+
}
21+
922
describe('<ShellOutput />', function () {
23+
beforeEach(cleanup);
24+
1025
it('renders no output lines if none are passed', function () {
11-
const wrapper = shallow(<ShellOutput output={[]} />);
12-
expect(wrapper.find(ShellOutputLine)).to.have.lengthOf(0);
26+
render(<WrappedShellOutput output={[]} />);
27+
expect(screen.queryByTestId('shell-output-line')).to.not.exist;
1328
});
1429

1530
it('renders an output line if one is passed', function () {
16-
const line1: ShellOutputEntry = { type: 'output', value: 'line 1' };
17-
const wrapper = shallow(<ShellOutput output={[line1]} />);
18-
expect(wrapper.find(ShellOutputLine)).to.have.lengthOf(1);
31+
const line1: ShellOutputEntry = {
32+
type: 'output',
33+
value: 'line 1',
34+
format: 'output',
35+
};
36+
render(<WrappedShellOutput output={[line1]} />);
37+
expect(screen.getByText(/line 1/i)).to.exist;
38+
expect(screen.getAllByTestId('shell-output-line')).to.have.lengthOf(1);
1939
});
2040

21-
it('renders no output lines if only one with a value of undefined is passed', function () {
22-
const line1: ShellOutputEntry = { type: 'output', value: undefined };
23-
const wrapper = shallow(<ShellOutput output={[line1]} />);
24-
expect(wrapper.find(ShellOutputLine)).to.have.lengthOf(0);
25-
});
41+
it('scrolls to the newly added item', async function () {
42+
const output: ShellOutputEntry[] = Array.from({ length: 100 }, (_, i) => ({
43+
type: 'output',
44+
value: `line ${i}`,
45+
format: 'output',
46+
}));
47+
48+
const { rerender } = render(<WrappedShellOutput output={output} />);
49+
50+
const newLine: ShellOutputEntry = {
51+
type: 'output',
52+
value: 'new line',
53+
format: 'output',
54+
};
2655

27-
it('pass the entry to the output line as prop', function () {
28-
const line1: ShellOutputEntry = { type: 'output', value: 'line 1' };
29-
const wrapper = shallow(<ShellOutput output={[line1]} />);
56+
rerender(<WrappedShellOutput output={[...output, newLine]} />);
3057

31-
expect(wrapper.find(ShellOutputLine).prop('entry')).to.deep.equal(line1);
58+
await waitFor(() => {
59+
expect(screen.getByText(/new line/i)).to.exist;
60+
});
3261
});
3362
});

packages/browser-repl/src/components/shell-output.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ export type { ShellOutputEntry } from './shell-output-line';
99
type ShellIOListProps = {
1010
output: ShellOutputEntry[];
1111
setInnerContainerRef: (ref: HTMLDivElement) => void;
12+
__TEST_LIST_HEIGHT?: number;
1213
};
1314

1415
export const ShellOutput = ({
1516
output,
1617
setInnerContainerRef,
18+
__TEST_LIST_HEIGHT,
1719
}: ShellIOListProps) => {
1820
const listRef: VirtualListRef = useRef();
1921

@@ -40,6 +42,7 @@ export const ShellOutput = ({
4042
</div>
4143
)}
4244
estimateItemInitialHeight={() => 24}
45+
__TEST_LIST_HEIGHT={__TEST_LIST_HEIGHT}
4346
/>
4447
);
4548
};

packages/browser-repl/src/components/shell.spec.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ function ShellWrapper({
2828
...props
2929
}: React.ComponentProps<typeof Shell>) {
3030
const initialEvaluate = useInitialEval(_initialEvaluate);
31-
return <Shell initialEvaluate={initialEvaluate} {...props} />;
31+
return (
32+
<div style={{ height: '100px', width: '100px' }}>
33+
<Shell initialEvaluate={initialEvaluate} {...props} />
34+
</div>
35+
);
3236
}
3337

3438
function filterEvaluateCalls(calls: any) {
@@ -89,8 +93,18 @@ describe('shell', function () {
8993
});
9094

9195
it('calls onOutputChanged', async function () {
92-
let output = [];
93-
const onOutputChanged = (newOutput) => {
96+
const shellEntries: ShellOutputEntry[] = Array.from(
97+
{ length: 10 },
98+
(_, i) => ({
99+
format: 'input',
100+
type: 'input',
101+
value: `item ${i}`,
102+
})
103+
);
104+
105+
let output = shellEntries;
106+
// this callback contains the new output (current merged with the new one)
107+
const onOutputChanged = (newOutput: ShellOutputEntry[]) => {
94108
output = newOutput;
95109
};
96110

@@ -100,12 +114,13 @@ describe('shell', function () {
100114
runtime={fakeRuntime}
101115
initialEvaluate={initialEvaluate}
102116
onOutputChanged={onOutputChanged}
103-
output={output}
117+
output={output as any}
104118
/>
105119
);
106120

107121
await waitFor(() => {
108122
expect(output).to.deep.equal([
123+
...shellEntries,
109124
{
110125
format: 'input',
111126
value: 'my command',
@@ -120,22 +135,17 @@ describe('shell', function () {
120135

121136
expect(filterEvaluateCalls(fakeRuntime.evaluate.args)).to.have.length(1);
122137

123-
// scrolls to the bottom initially and every time it outputs
124-
await waitFor(() => {
125-
expect(Element.prototype.scrollIntoView).to.have.been.calledTwice;
126-
});
127-
128-
// make sure we scroll to the bottom every time output changes
129138
rerender(
130139
<ShellWrapper
131140
runtime={fakeRuntime}
132141
initialEvaluate={initialEvaluate}
133142
onOutputChanged={onOutputChanged}
134-
output={output}
143+
output={output as any}
135144
/>
136145
);
146+
// Make sure that it scrolls to the last added item
137147
await waitFor(() => {
138-
expect(Element.prototype.scrollIntoView).to.have.been.calledThrice;
148+
expect(screen.getByText('some result')).to.exist;
139149
});
140150
});
141151

packages/browser-repl/src/components/shell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
485485
return () => {
486486
observer.disconnect();
487487
};
488-
}, [output]);
488+
}, [listInnerContainerRef.current]);
489489

490490
useEffect(() => {
491491
if (!shellInputContainerRef.current) {

0 commit comments

Comments
 (0)