Skip to content

Commit 56b4c54

Browse files
committed
Increase test coverage for workspace/Repl.tsx
- Add a mockTypeError in mocks for situations that require a mock SourceError of some kind.
1 parent 6948426 commit 56b4c54

File tree

3 files changed

+142
-7
lines changed

3 files changed

+142
-7
lines changed
Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
import { shallow } from 'enzyme'
22
import * as React from 'react'
33

4-
import { ResultOutput } from '../../../reducers/states'
4+
import { mockTypeError } from '../../../mocks/context'
55
import Repl, { Output } from '../Repl'
66

7+
const mockRunningOutput = {
8+
type: 'running',
9+
consoleLogs: ['a', 'bb', 'cccccccccccccccccccccccccccccccc', 'd']
10+
}
11+
12+
const mockCodeOutput = {
13+
type: 'code',
14+
value: "display('');"
15+
}
16+
17+
const mockResultOutput = {
18+
type: 'result',
19+
value: 42,
20+
consoleLogs: []
21+
}
22+
23+
const mockErrorOutput = {
24+
type: 'errors',
25+
errors: [mockTypeError()],
26+
consoleLogs: []
27+
}
28+
729
test('Repl renders correctly', () => {
830
const props = {
9-
output: [{ type: 'result', value: 'abc', consoleLogs: [] } as ResultOutput],
31+
output: [mockResultOutput, mockCodeOutput, mockErrorOutput, mockRunningOutput],
1032
replValue: '',
1133
handleReplValueChange: (newCode: string) => {},
1234
handleReplEval: () => {},
@@ -18,9 +40,52 @@ test('Repl renders correctly', () => {
1840
expect(tree.debug()).toMatchSnapshot()
1941
})
2042

21-
test("Output renders correctly for InterpreterOutput.type === 'result'", () => {
22-
const props: ResultOutput = { type: 'result', value: 'def', consoleLogs: [] }
43+
test('Code output renders correctly', () => {
44+
const app = <Output {...{ output: mockCodeOutput }} />
45+
const tree = shallow(app)
46+
expect(tree.debug()).toMatchSnapshot()
47+
})
48+
49+
test('Running output renders correctly', () => {
50+
const app = <Output {...{ output: mockRunningOutput }} />
51+
const tree = shallow(app)
52+
expect(tree.debug()).toMatchSnapshot()
53+
})
54+
55+
test('Result output (no consoleLogs) renders correctly', () => {
56+
const app = <Output {...{ output: mockResultOutput }} />
57+
const tree = shallow(app)
58+
expect(tree.debug()).toMatchSnapshot()
59+
})
60+
61+
test('Result output (with consoleLogs) renders correctly', () => {
62+
const props = {
63+
...mockResultOutput,
64+
consoleLogs: mockRunningOutput.consoleLogs
65+
}
66+
const app = <Output {...{ output: props }} />
67+
const tree = shallow(app)
68+
expect(tree.debug()).toMatchSnapshot()
69+
})
70+
71+
test('Error output (no consoleLogs) renders correctly', () => {
72+
const app = <Output {...{ output: mockErrorOutput }} />
73+
const tree = shallow(app)
74+
expect(tree.debug()).toMatchSnapshot()
75+
})
76+
77+
test('Error output (with consoleLogs) renders correctly', () => {
78+
const props = {
79+
...mockErrorOutput,
80+
consoleLogs: mockRunningOutput.consoleLogs
81+
}
2382
const app = <Output {...{ output: props }} />
2483
const tree = shallow(app)
2584
expect(tree.debug()).toMatchSnapshot()
2685
})
86+
87+
test('Empty output renders an empty card', () => {
88+
const app = <Output {...{ output: {} }} />
89+
const tree = shallow(app)
90+
expect(tree.debug()).toMatchSnapshot()
91+
})
Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Output renders correctly for InterpreterOutput.type === 'result' 1`] = `
3+
exports[`Code output renders correctly 1`] = `
44
"<Blueprint2.Card elevation={0} interactive={false}>
5-
<pre className=\\"resultOutput\\">
6-
&quot;def&quot;
5+
<pre className=\\"codeOutput\\">
6+
display(&#39;&#39;);
7+
</pre>
8+
</Blueprint2.Card>"
9+
`;
10+
11+
exports[`Empty output renders an empty card 1`] = `
12+
"<Blueprint2.Card elevation={0} interactive={false}>
13+
&#39;&#39;
14+
</Blueprint2.Card>"
15+
`;
16+
17+
exports[`Error output (no consoleLogs) renders correctly 1`] = `
18+
"<Blueprint2.Card elevation={0} interactive={false}>
19+
<pre className=\\"errorOutput\\">
20+
Line &lt;unknown&gt;: Expected , got .
21+
</pre>
22+
</Blueprint2.Card>"
23+
`;
24+
25+
exports[`Error output (with consoleLogs) renders correctly 1`] = `
26+
"<Blueprint2.Card elevation={0} interactive={false}>
27+
<pre className=\\"logOutput\\">
28+
a
29+
bb
30+
cccccccccccccccccccccccccccccccc
31+
d
32+
</pre>
33+
<br />
34+
<pre className=\\"errorOutput\\">
35+
Line &lt;unknown&gt;: Expected , got .
736
</pre>
837
</Blueprint2.Card>"
938
`;
@@ -14,10 +43,46 @@ exports[`Repl renders correctly 1`] = `
1443
<ReplControl output={{...}} replValue=\\"\\" handleReplValueChange={[Function: handleReplValueChange]} handleReplEval={[Function: handleReplEval]} handleReplOutputClear={[Function: handleReplOutputClear]} handleChapterSelect={[Function: handleChapterSelect]} />
1544
</div>
1645
<div className=\\"repl-output-parent\\">
46+
<Component output={{...}} />
47+
<Component output={{...}} />
48+
<Component output={{...}} />
1749
<Component output={{...}} />
1850
<div className=\\"repl-input-parent row\\">
1951
<ReplInput output={{...}} replValue=\\"\\" handleReplValueChange={[Function: handleReplValueChange]} handleReplEval={[Function: handleReplEval]} handleReplOutputClear={[Function: handleReplOutputClear]} handleChapterSelect={[Function: handleChapterSelect]} />
2052
</div>
2153
</div>
2254
</div>"
2355
`;
56+
57+
exports[`Result output (no consoleLogs) renders correctly 1`] = `
58+
"<Blueprint2.Card elevation={0} interactive={false}>
59+
<pre className=\\"resultOutput\\">
60+
42
61+
</pre>
62+
</Blueprint2.Card>"
63+
`;
64+
65+
exports[`Result output (with consoleLogs) renders correctly 1`] = `
66+
"<Blueprint2.Card elevation={0} interactive={false}>
67+
<pre className=\\"logOutput\\">
68+
a
69+
bb
70+
cccccccccccccccccccccccccccccccc
71+
d
72+
</pre>
73+
<pre className=\\"resultOutput\\">
74+
42
75+
</pre>
76+
</Blueprint2.Card>"
77+
`;
78+
79+
exports[`Running output renders correctly 1`] = `
80+
"<Blueprint2.Card elevation={0} interactive={false}>
81+
<pre className=\\"logOutput\\">
82+
a
83+
bb
84+
cccccccccccccccccccccccccccccccc
85+
d
86+
</pre>
87+
</Blueprint2.Card>"
88+
`;

src/mocks/context.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as es from 'estree'
22

33
import { createContext } from '../slang'
44
import { Closure, Context, Frame } from '../slang/types'
5+
import { TypeError } from '../slang/utils/rttc'
56

67
export function mockContext(chapter = 1): Context {
78
return createContext(chapter)
@@ -31,3 +32,7 @@ export function mockRuntimeContext(): Context {
3132
export function mockClosure(): Closure {
3233
return new Closure({} as es.FunctionExpression, {} as Frame, {} as Context)
3334
}
35+
36+
export function mockTypeError(): TypeError {
37+
return new TypeError({ loc: 0 } as es.Node, '', '', '')
38+
}

0 commit comments

Comments
 (0)