-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
feat: migrate codebox component #5330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ovflowd
merged 50 commits into
nodejs:major/website-redesign
from
araujogui:feat/codebox
May 18, 2023
Merged
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
0a1bb4b
feat: create codebox component
araujogui 57d9a47
feat: add pointer to copy button
araujogui 014ccb0
feat: add shell support
araujogui 73d60cc
feat: use code element instead of pre
araujogui e73b588
chore: fix eslint issues
araujogui ce6bcc6
refactor: remove shell codebox
araujogui 2579f51
refactor: use react.fc type
araujogui 162b25e
refactor: remove duplicate encoding
araujogui 420da72
refactor: use pre element for codebox
araujogui dbfb453
style: some eol
araujogui 5cf21f5
style: some eol
araujogui 15c4ead
refactor: use pre element for codebox
araujogui 61f1eca
chore: create inlinecode story
araujogui d84efea
refactor: create inline code story type
araujogui a2a093b
chore: revert .browserslistrc
araujogui 3ce8d41
test: remove duplicated test
araujogui 0315098
test: remove beginning space on code
araujogui c176fb6
refactor: setup userEvent on tests
araujogui e1642d8
refactor: remove useless async
araujogui 8044a7f
test: manual mock isomorphic-dompurify
araujogui 07f129f
fix: some fixes
araujogui d1d63e9
chore: create multi lang story
araujogui f34a78c
test: snapshot before and after switch lang
araujogui be54384
refactor: inline mock
araujogui 375c5b3
refactor: use decorators
araujogui 60af3bd
chore: split inlinecode component
araujogui 968b042
feat: add selected lang state
araujogui ddf3cb6
refactor: add inlinecode decorator
araujogui 2e7cf24
refactor: remove duplicate classes
araujogui 9ee2cb1
chore: revert .browserslistrc
araujogui c9de0d2
test: fix tests
araujogui 7acf4d5
chore: wip
araujogui 0c0c41a
feat: use story component arg
araujogui d12e5ab
style: stories
araujogui 89b0e80
test: remove i18n from clipboard test
araujogui 7b9d637
chore: migrate pagination component (#5331)
ktssr 88e69cc
chore: add a proper decorated component
ovflowd f2c19ff
refactor: decorated inline code
araujogui 62e129b
Chore(node feat) (#5338)
AugustinMauroy acbf58f
(website redesign) Feat(shellbox): migration (#5234)
AugustinMauroy 5072fce
fix(i18n): translation key (#5347)
AugustinMauroy 78b6461
feat: add hover states
araujogui a4fdf40
chore: merge major/website-redesign
araujogui 1c131a1
chore: fix rebase leftovers
araujogui 0cfc4c4
chore: update snapshots
araujogui bf6c9e1
chore: install @types/prismjs
araujogui 2c40303
chore: merge major/website-redesign
araujogui bb731f3
Update index.stories.tsx
ovflowd 099af8b
chore: remove duplicate snapshots
araujogui 100fd00
chore: remove duplicate snapshot
araujogui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -23,6 +23,7 @@ const preview: Preview = { | |
basePath: '', | ||
}, | ||
}, | ||
backgrounds: { disable: true }, | ||
}, | ||
}; | ||
|
||
|
83 changes: 83 additions & 0 deletions
83
components/Article/Codebox/__snapshots__/index.stories.tsx.snap
This file contains hidden or 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,83 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Article/Codebox Default smoke-test 1`] = ` | ||
<pre class="Codebox_pre___vcVQ language-js"> | ||
<div class="Codebox_top__1r10k"> | ||
<div class="Codebox_langBox__zXBm7"> | ||
<button type="button" | ||
class="Codebox_lang__X6WDB Codebox_selected__gFZLU" | ||
> | ||
js | ||
</button> | ||
</div> | ||
<button type="button" | ||
class="Codebox_copy__ZEctM" | ||
> | ||
copy | ||
</button> | ||
</div> | ||
<div class="Codebox_content__jhl_a"> | ||
<span class="token keyword"> | ||
const | ||
</span> | ||
a | ||
<span class="token operator"> | ||
= | ||
</span> | ||
<span class="token number"> | ||
1 | ||
</span> | ||
<span class="token punctuation"> | ||
; | ||
</span> | ||
</div> | ||
</pre> | ||
`; | ||
|
||
exports[`Article/Codebox MultiLang smoke-test 1`] = ` | ||
<pre class="Codebox_pre___vcVQ language-js|language-mjs"> | ||
<div class="Codebox_top__1r10k"> | ||
<div class="Codebox_langBox__zXBm7"> | ||
<button type="button" | ||
class="Codebox_lang__X6WDB Codebox_selected__gFZLU" | ||
> | ||
cjs | ||
</button> | ||
<button type="button" | ||
class="Codebox_lang__X6WDB" | ||
> | ||
mjs | ||
</button> | ||
</div> | ||
<button type="button" | ||
class="Codebox_copy__ZEctM" | ||
> | ||
copy | ||
</button> | ||
</div> | ||
<div class="Codebox_content__jhl_a"> | ||
<span class="token keyword"> | ||
const | ||
</span> | ||
http | ||
<span class="token operator"> | ||
= | ||
</span> | ||
<span class="token function"> | ||
require | ||
</span> | ||
<span class="token punctuation"> | ||
( | ||
</span> | ||
<span class="token string"> | ||
'http' | ||
</span> | ||
<span class="token punctuation"> | ||
) | ||
</span> | ||
<span class="token punctuation"> | ||
; | ||
</span> | ||
</div> | ||
</pre> | ||
`; |
140 changes: 140 additions & 0 deletions
140
components/Article/Codebox/__tests__/__snapshots__/index.test.tsx.snap
This file contains hidden or 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,140 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Codebox component (multiple langs) switch between languages 1`] = ` | ||
<div> | ||
<pre | ||
class="pre language-js|language-mjs" | ||
> | ||
<div | ||
class="top" | ||
> | ||
<div | ||
class="langBox" | ||
> | ||
<button | ||
class="lang selected" | ||
type="button" | ||
> | ||
cjs | ||
</button> | ||
<button | ||
class="lang" | ||
type="button" | ||
> | ||
mjs | ||
</button> | ||
</div> | ||
<button | ||
class="copy" | ||
type="button" | ||
> | ||
components.codeBox.copy | ||
</button> | ||
</div> | ||
<div | ||
class="content" | ||
> | ||
<span | ||
class="token keyword" | ||
> | ||
const | ||
</span> | ||
http | ||
<span | ||
class="token operator" | ||
> | ||
= | ||
</span> | ||
|
||
<span | ||
class="token function" | ||
> | ||
require | ||
</span> | ||
<span | ||
class="token punctuation" | ||
> | ||
( | ||
</span> | ||
<span | ||
class="token string" | ||
> | ||
'http' | ||
</span> | ||
<span | ||
class="token punctuation" | ||
> | ||
) | ||
</span> | ||
<span | ||
class="token punctuation" | ||
> | ||
; | ||
</span> | ||
|
||
|
||
</div> | ||
</pre> | ||
</div> | ||
`; | ||
|
||
exports[`Codebox component (multiple langs) switch between languages 2`] = ` | ||
<div> | ||
<pre | ||
class="pre language-js|language-mjs" | ||
> | ||
<div | ||
class="top" | ||
> | ||
<div | ||
class="langBox" | ||
> | ||
<button | ||
class="lang" | ||
type="button" | ||
> | ||
cjs | ||
</button> | ||
<button | ||
class="lang selected" | ||
type="button" | ||
> | ||
mjs | ||
</button> | ||
</div> | ||
<button | ||
class="copy" | ||
type="button" | ||
> | ||
components.codeBox.copy | ||
</button> | ||
</div> | ||
<div | ||
class="content" | ||
> | ||
<span | ||
class="token keyword" | ||
> | ||
import | ||
</span> | ||
http | ||
<span | ||
class="token keyword" | ||
> | ||
from | ||
</span> | ||
|
||
<span | ||
class="token string" | ||
> | ||
'http' | ||
</span> | ||
<span | ||
class="token punctuation" | ||
> | ||
; | ||
</span> | ||
</div> | ||
</pre> | ||
</div> | ||
`; |
This file contains hidden or 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,77 @@ | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { IntlProvider } from 'react-intl'; | ||
|
||
import Codebox, { replaceLabelLanguages, replaceLanguages } from '../index'; | ||
|
||
jest.mock('isomorphic-dompurify', () => { | ||
return { | ||
sanitize: jest.fn().mockImplementation(source => source), | ||
}; | ||
}); | ||
|
||
describe('Replacer tests', (): void => { | ||
it('replaceLabelLanguages', (): void => { | ||
expect(replaceLabelLanguages('language-console')).toBe('language-bash'); | ||
}); | ||
|
||
it('replaceLanguages', (): void => { | ||
expect(replaceLanguages('language-mjs')).toBe('language-js'); | ||
expect(replaceLanguages('language-cjs')).toBe('language-js'); | ||
expect(replaceLanguages('language-javascript')).toBe('language-js'); | ||
expect(replaceLanguages('language-console')).toBe('language-bash'); | ||
expect(replaceLanguages('language-shell')).toBe('language-bash'); | ||
}); | ||
}); | ||
|
||
describe('Codebox component (one lang)', (): void => { | ||
const code = 'const a = 1;'; | ||
|
||
it('should copy content', async () => { | ||
const user = userEvent.setup(); | ||
|
||
render( | ||
<IntlProvider locale="en" onError={() => {}}> | ||
<Codebox> | ||
<pre className="language-js">{code}</pre> | ||
</Codebox> | ||
</IntlProvider> | ||
); | ||
|
||
const navigatorClipboardWriteTextSpy = jest.spyOn( | ||
navigator.clipboard, | ||
'writeText' | ||
); | ||
|
||
const buttonElement = screen.getByText('components.codeBox.copy'); | ||
await user.click(buttonElement); | ||
|
||
expect(navigatorClipboardWriteTextSpy).toHaveBeenCalledTimes(1); | ||
expect(navigatorClipboardWriteTextSpy).toHaveBeenCalledWith(code); | ||
}); | ||
}); | ||
|
||
describe('Codebox component (multiple langs)', (): void => { | ||
const code = `const http = require('http'); | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-------------- | ||
import http from 'http';`; | ||
|
||
it('switch between languages', async () => { | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const user = userEvent.setup(); | ||
|
||
const { container } = render( | ||
<IntlProvider locale="en" onError={() => {}}> | ||
<Codebox> | ||
<pre className="language-cjs|language-mjs">{code}</pre> | ||
</Codebox> | ||
</IntlProvider> | ||
); | ||
|
||
expect(container).toMatchSnapshot(); | ||
|
||
const buttonElement = await screen.findByText('mjs'); | ||
await user.click(buttonElement); | ||
|
||
expect(container).toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.