-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: restore older impl of CodeViewer (#168)
* fix: restore older impl of CodeViewer * chore: move directories * chore: langs * fix: types and stuff * fix: add refractor dep * revert: directory structure * test: refractor is typed now
- Loading branch information
Showing
17 changed files
with
1,128 additions
and
36 deletions.
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 |
---|---|---|
@@ -1,35 +1,77 @@ | ||
import { mount } from 'enzyme'; | ||
import { shallow } from 'enzyme'; | ||
import 'jest-enzyme'; | ||
import * as React from 'react'; | ||
import { CodeViewer } from '..'; | ||
import { CodeViewer } from '../index'; | ||
import { astToReact } from '../utils/astToReact'; | ||
import { parseCode } from '../utils/parseCode'; | ||
|
||
jest.mock('../utils/astToReact'); | ||
jest.mock('../utils/parseCode'); | ||
|
||
describe('Code Viewer component', () => { | ||
afterEach(() => { | ||
(parseCode as jest.Mock).mockReset(); | ||
(astToReact as jest.Mock).mockReset(); | ||
}); | ||
|
||
it('renders code element with raw value for inline view', () => { | ||
const code = '{}'; | ||
const language = 'json'; | ||
|
||
const wrapper = mount(<CodeViewer language={language} value={code} inline />); | ||
expect(wrapper).toHaveHTML( | ||
`<code class="bp3-code-editor isInline"><span class="token punctuation">{</span><span class="token punctuation">}</span></code>`, | ||
); | ||
const wrapper = shallow(<CodeViewer language={language} value={code} inline />); | ||
expect(wrapper).toHaveText(code); | ||
expect(wrapper).toHaveDisplayName('code'); | ||
|
||
expect(parseCode).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('renders pre element for block view', () => { | ||
const code = '{}'; | ||
const language = 'json'; | ||
|
||
const wrapper = mount(<CodeViewer language={language} value={code} />); | ||
expect(wrapper).toHaveHTML( | ||
`<pre class="bp3-code-editor language-json"><span class="token punctuation">{</span><span class="token punctuation">}</span></pre>`, | ||
); | ||
const wrapper = shallow(<CodeViewer language={language} value={code} />); | ||
expect(wrapper).toHaveDisplayName('pre'); | ||
}); | ||
|
||
it('renders code as is if parsing fails', () => { | ||
const code = '{}'; | ||
const language = 'json'; | ||
(parseCode as jest.Mock).mockReturnValue(null); | ||
|
||
const wrapper = shallow(<CodeViewer language={language} value={code} />); | ||
expect(wrapper).toHaveText(code); | ||
|
||
expect(parseCode).toHaveBeenCalledWith(code, language, false); | ||
}); | ||
|
||
it('does not try to map ast nodes to react nodes if parsing failed', () => { | ||
(parseCode as jest.Mock).mockReturnValue(null); | ||
shallow(<CodeViewer language="javascript" value="foo()" />); | ||
|
||
expect(astToReact).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('renders parsed markup if possible', () => { | ||
const code = 'function'; | ||
const language = 'javascript'; | ||
const html = `<pre class="bp3-code-editor language-javascript"><span class="token keyword">function</span></pre>`; | ||
const ast = [ | ||
{ | ||
type: 'element', | ||
tagName: 'span', | ||
properties: { | ||
className: ['token', 'function'], | ||
}, | ||
value: 'function', | ||
}, | ||
]; | ||
const markup = <span className="token function">function</span>; | ||
|
||
(parseCode as jest.Mock).mockReturnValue(ast); | ||
(astToReact as jest.Mock).mockReturnValue(() => markup); | ||
|
||
const wrapper = mount(<CodeViewer language={language} value={code} />); | ||
expect(wrapper).toHaveHTML(html); | ||
const wrapper = shallow(<CodeViewer language={language} value={code} />); | ||
expect(wrapper).toContainReact(markup); | ||
expect(parseCode).toHaveBeenCalledWith(code, language, false); | ||
expect(astToReact).toHaveBeenCalled(); | ||
}); | ||
}); |
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 was deleted.
Oops, something went wrong.
118 changes: 118 additions & 0 deletions
118
src/CodeViewer/utils/__tests__/__snapshots__/astToReact.spec.ts.snap
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,118 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`astToReact util fixture clike 1`] = ` | ||
<pre> | ||
int | ||
<span | ||
className="token function" | ||
key="cv-0-1" | ||
> | ||
main | ||
</span> | ||
<span | ||
className="token punctuation" | ||
key="cv-0-2" | ||
> | ||
( | ||
</span> | ||
<span | ||
className="token punctuation" | ||
key="cv-0-3" | ||
> | ||
) | ||
</span> | ||
<span | ||
className="token punctuation" | ||
key="cv-0-5" | ||
> | ||
{ | ||
</span> | ||
<span | ||
className="token comment" | ||
key="cv-0-7" | ||
> | ||
// some dumb code | ||
</span> | ||
<span | ||
className="token keyword" | ||
key="cv-0-9" | ||
> | ||
return | ||
</span> | ||
<span | ||
className="token number" | ||
key="cv-0-11" | ||
> | ||
0 | ||
</span> | ||
<span | ||
className="token punctuation" | ||
key="cv-0-12" | ||
> | ||
; | ||
</span> | ||
<span | ||
className="token punctuation" | ||
key="cv-0-14" | ||
> | ||
} | ||
</span> | ||
</pre> | ||
`; | ||
exports[`astToReact util fixture javascript 1`] = ` | ||
<pre> | ||
<span | ||
className="token keyword" | ||
key="cv-0-0" | ||
> | ||
const | ||
</span> | ||
defaultValue | ||
<span | ||
className="token operator" | ||
key="cv-0-2" | ||
> | ||
= | ||
</span> | ||
stoplight | ||
<span | ||
className="token punctuation" | ||
key="cv-0-4" | ||
> | ||
. | ||
</span> | ||
<span | ||
className="token function" | ||
key="cv-0-5" | ||
> | ||
io | ||
</span> | ||
<span | ||
className="token punctuation" | ||
key="cv-0-6" | ||
> | ||
( | ||
</span> | ||
<span | ||
className="token punctuation" | ||
key="cv-0-7" | ||
> | ||
) | ||
</span> | ||
<span | ||
className="token punctuation" | ||
key="cv-0-8" | ||
> | ||
; | ||
</span> | ||
</pre> | ||
`; |
Oops, something went wrong.