@codecademy/codebytes 0.2.1-alpha.ea0d6d.0 → 0.3.1-alpha.166554.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/package.json +3 -6
- package/src/consts.ts +1 -1
- package/src/editor.tsx +6 -9
- package/src/index.tsx +4 -13
- package/src/types.ts +4 -4
- package/jest.config.js +0 -1
- package/src/__tests__/codebyte-test.tsx +0 -71
- package/src/__tests__/editor-test.tsx +0 -59
- package/src/__tests__/language-selection-test.tsx +0 -14
package/CHANGELOG.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
-
### [0.
|
|
6
|
+
### [0.3.1-alpha.166554.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.3.1-alpha.166554.0) (2022-01-14)
|
|
7
7
|
|
|
8
8
|
**Note:** Version bump only for package @codecademy/codebytes
|
|
9
9
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codecademy/codebytes",
|
|
3
3
|
"description": "Codebytes Code Editor",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.1-alpha.166554.0",
|
|
5
5
|
"author": "Codecademy Engineering <dev@codecademy.com>",
|
|
6
6
|
"sideEffects": [
|
|
7
7
|
"**/*.css",
|
|
@@ -40,16 +40,13 @@
|
|
|
40
40
|
},
|
|
41
41
|
"license": "MIT",
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@codecademy/gamut-tests": "*",
|
|
44
43
|
"@emotion/jest": "^11.3.0",
|
|
45
44
|
"@testing-library/dom": "^7.31.2",
|
|
46
45
|
"@testing-library/react": "^11.0.4",
|
|
47
|
-
"@
|
|
48
|
-
"@testing-library/user-event": "13.1.1",
|
|
49
|
-
"monaco-editor-webpack-plugin": "1.9.1"
|
|
46
|
+
"@types/loadable__component": "^5.13.2"
|
|
50
47
|
},
|
|
51
48
|
"publishConfig": {
|
|
52
49
|
"access": "public"
|
|
53
50
|
},
|
|
54
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "a34aa58208dd2d113d885cf0b6679536979be8fd"
|
|
55
52
|
}
|
package/src/consts.ts
CHANGED
|
@@ -16,7 +16,7 @@ export type languageOption = keyof typeof languageOptions;
|
|
|
16
16
|
|
|
17
17
|
export const validLanguages = Object.keys(languageOptions).filter(
|
|
18
18
|
(option) => !!option
|
|
19
|
-
) as
|
|
19
|
+
) as languageOption[];
|
|
20
20
|
|
|
21
21
|
const cpp = `#include <iostream>
|
|
22
22
|
int main() {
|
package/src/editor.tsx
CHANGED
|
@@ -57,8 +57,10 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
57
57
|
const [isCodeByteCopied, setIsCodeByteCopied] = useState(false);
|
|
58
58
|
const onCopyClick = () => {
|
|
59
59
|
if (!isCodeByteCopied) {
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
navigator.clipboard
|
|
61
|
+
.writeText(text)
|
|
62
|
+
// eslint-disable-next-line no-console
|
|
63
|
+
.catch(() => console.error('Failed to copy'));
|
|
62
64
|
on.copy?.(text, language);
|
|
63
65
|
setIsCodeByteCopied(true);
|
|
64
66
|
}
|
|
@@ -130,20 +132,15 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
130
132
|
variant="secondary"
|
|
131
133
|
onClick={onCopyClick}
|
|
132
134
|
onBlur={() => setIsCodeByteCopied(false)}
|
|
133
|
-
data-testid="copy-codebyte-btn"
|
|
134
135
|
>
|
|
135
136
|
<CopyIconStyled aria-hidden="true" /> Copy Codebyte
|
|
136
137
|
</TextButton>
|
|
137
138
|
}
|
|
138
139
|
>
|
|
139
140
|
{isCodeByteCopied ? (
|
|
140
|
-
<span
|
|
141
|
-
Copied!
|
|
142
|
-
</span>
|
|
141
|
+
<span role="alert">Copied!</span>
|
|
143
142
|
) : (
|
|
144
|
-
<span
|
|
145
|
-
Copy to your clipboard
|
|
146
|
-
</span>
|
|
143
|
+
<span>Copy to your clipboard</span>
|
|
147
144
|
)}
|
|
148
145
|
</ToolTip>
|
|
149
146
|
) : null}
|
package/src/index.tsx
CHANGED
|
@@ -3,7 +3,7 @@ import { FaviconIcon } from '@codecademy/gamut-icons';
|
|
|
3
3
|
import { Background, states, system } from '@codecademy/gamut-styles';
|
|
4
4
|
import { StyleProps } from '@codecademy/variance';
|
|
5
5
|
import styled from '@emotion/styled';
|
|
6
|
-
import React, {
|
|
6
|
+
import React, { useState } from 'react';
|
|
7
7
|
|
|
8
8
|
import { helloWorld, languageOption } from './consts';
|
|
9
9
|
import { Editor } from './editor';
|
|
@@ -35,22 +35,13 @@ const EditorContainer = styled(Background)<EditorStyleProps>(
|
|
|
35
35
|
export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
36
36
|
text: initialText,
|
|
37
37
|
language: initialLanguage,
|
|
38
|
-
hideCopyButton
|
|
38
|
+
hideCopyButton,
|
|
39
39
|
isIFrame = false,
|
|
40
40
|
on = {},
|
|
41
41
|
snippetsBaseUrl = process.env.CONTAINER_API_BASE,
|
|
42
42
|
}) => {
|
|
43
|
-
const [text, setText] = useState<string>(initialText
|
|
44
|
-
const [language, setLanguage] = useState<languageOption>(
|
|
45
|
-
initialLanguage ?? ''
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
useEffect(() => {
|
|
49
|
-
if (language) {
|
|
50
|
-
setText(initialText ?? (helloWorld[language] || ''));
|
|
51
|
-
}
|
|
52
|
-
}, [language, initialText]);
|
|
53
|
-
|
|
43
|
+
const [text, setText] = useState<string>(initialText);
|
|
44
|
+
const [language, setLanguage] = useState<languageOption>(initialLanguage);
|
|
54
45
|
return (
|
|
55
46
|
<EditorContainer bg="black" as="main" isIFrame={isIFrame}>
|
|
56
47
|
<Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
|
package/src/types.ts
CHANGED
|
@@ -11,10 +11,10 @@ export type CodebytesChangeHandlerMap = {
|
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
export interface CodeByteEditorProps {
|
|
14
|
-
text
|
|
15
|
-
language
|
|
16
|
-
hideCopyButton
|
|
14
|
+
text: string;
|
|
15
|
+
language: languageOption;
|
|
16
|
+
hideCopyButton: boolean;
|
|
17
17
|
isIFrame?: boolean;
|
|
18
|
-
snippetsBaseUrl?: string
|
|
18
|
+
snippetsBaseUrl?: string;
|
|
19
19
|
on?: CodebytesChangeHandlerMap;
|
|
20
20
|
}
|
package/jest.config.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('../../jest.config.base')('codebytes');
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { setupRtl } from '@codecademy/gamut-tests';
|
|
2
|
-
import userEvent from '@testing-library/user-event';
|
|
3
|
-
import React from 'react';
|
|
4
|
-
|
|
5
|
-
import { CodeByteEditor } from '..';
|
|
6
|
-
import { helloWorld, validLanguages } from '../consts';
|
|
7
|
-
|
|
8
|
-
const mockEditorTestId = 'mock-editor-test-id';
|
|
9
|
-
|
|
10
|
-
jest.mock('react-resize-observer');
|
|
11
|
-
// This is a super simplified mock capable of render value and trigger onChange.
|
|
12
|
-
jest.mock('../MonacoEditor', () => ({
|
|
13
|
-
SimpleMonacoEditor: ({
|
|
14
|
-
value,
|
|
15
|
-
onChange,
|
|
16
|
-
}: {
|
|
17
|
-
value: string;
|
|
18
|
-
onChange?: (value: string) => void;
|
|
19
|
-
}) => (
|
|
20
|
-
<>
|
|
21
|
-
{value}
|
|
22
|
-
<input
|
|
23
|
-
data-testid={mockEditorTestId}
|
|
24
|
-
type="text"
|
|
25
|
-
onChange={(e) => {
|
|
26
|
-
onChange?.(e.target.value);
|
|
27
|
-
}}
|
|
28
|
-
value={value}
|
|
29
|
-
/>
|
|
30
|
-
</>
|
|
31
|
-
),
|
|
32
|
-
}));
|
|
33
|
-
|
|
34
|
-
const renderWrapper = setupRtl(CodeByteEditor, {});
|
|
35
|
-
|
|
36
|
-
describe('CodeBytes', () => {
|
|
37
|
-
it('has a language-specific "hello world" program defined for each language', () => {
|
|
38
|
-
validLanguages.forEach((language) => {
|
|
39
|
-
expect(helloWorld[language]).toBeDefined();
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('initializes with a language-specific "hello world" program when there is no language prop', () => {
|
|
44
|
-
const { view } = renderWrapper();
|
|
45
|
-
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
46
|
-
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
47
|
-
view.getByText(helloWorld.javascript);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('initializes with a language-specific "hello world" program when there is a language prop but no text prop', () => {
|
|
51
|
-
const { view } = renderWrapper({ language: 'javascript' });
|
|
52
|
-
view.getByText(helloWorld.javascript);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('initializes with deserialized text when there is a text prop but no language prop', () => {
|
|
56
|
-
const testString = 'yes hello';
|
|
57
|
-
const { view } = renderWrapper({ text: testString });
|
|
58
|
-
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
59
|
-
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
60
|
-
view.getByText(testString);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('initializes with deserialized text when there is both a language and text prop', () => {
|
|
64
|
-
const testString = 'yes hello';
|
|
65
|
-
const { view } = renderWrapper({
|
|
66
|
-
text: testString,
|
|
67
|
-
language: 'javascript',
|
|
68
|
-
});
|
|
69
|
-
view.getByText(testString);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { setupRtl } from '@codecademy/gamut-tests';
|
|
2
|
-
import userEvent from '@testing-library/user-event';
|
|
3
|
-
import React from 'react';
|
|
4
|
-
|
|
5
|
-
import { Editor } from '../editor';
|
|
6
|
-
|
|
7
|
-
jest.mock('../MonacoEditor', () => ({
|
|
8
|
-
SimpleMonacoEditor: ({ value }: { value: string }) => <>{value}</>,
|
|
9
|
-
}));
|
|
10
|
-
|
|
11
|
-
jest.mock('react-resize-observer');
|
|
12
|
-
|
|
13
|
-
const renderWrapper = setupRtl(Editor, {
|
|
14
|
-
hideCopyButton: false,
|
|
15
|
-
language: 'javascript',
|
|
16
|
-
text: 'hello world',
|
|
17
|
-
onChange: jest.fn(),
|
|
18
|
-
snippetsBaseUrl: '',
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
Object.defineProperty(navigator, 'clipboard', {
|
|
22
|
-
value: {
|
|
23
|
-
writeText: jest.fn(),
|
|
24
|
-
},
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
describe('Editor', () => {
|
|
28
|
-
(global as any).fetch = jest.fn();
|
|
29
|
-
afterEach(() => {
|
|
30
|
-
(global as any).fetch.mockClear();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('shows a prompt tooltip when the CodeByte has __not__ been copied via the button', () => {
|
|
34
|
-
const { view } = renderWrapper();
|
|
35
|
-
expect(view.queryByTestId('copy-confirmation-tooltip')).toBeFalsy();
|
|
36
|
-
view.getByTestId('copy-prompt-tooltip');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('shows a confirmation tooltip when the CodeByte has been copied via the button', () => {
|
|
40
|
-
const { view } = renderWrapper();
|
|
41
|
-
const copyBtn = view.getByTestId('copy-codebyte-btn');
|
|
42
|
-
userEvent.click(copyBtn as HTMLButtonElement);
|
|
43
|
-
expect(view.queryByTestId('copy-prompt-tooltip')).toBeFalsy();
|
|
44
|
-
view.getByTestId('copy-confirmation-tooltip');
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('hides the copy codebyte button if hideCopyButton prop is true"', () => {
|
|
48
|
-
const { view } = renderWrapper({
|
|
49
|
-
hideCopyButton: true,
|
|
50
|
-
});
|
|
51
|
-
expect(view.queryByTestId('copy-codebyte-btn')).toBeNull();
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('shows the copy codebyte button if hideCopyButton prop is not set', () => {
|
|
55
|
-
const { view } = renderWrapper();
|
|
56
|
-
|
|
57
|
-
view.getByTestId('copy-codebyte-btn');
|
|
58
|
-
});
|
|
59
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { setupRtl } from '@codecademy/gamut-tests';
|
|
2
|
-
|
|
3
|
-
import { LanguageSelection } from '../languageSelection';
|
|
4
|
-
|
|
5
|
-
const renderWrapper = setupRtl(LanguageSelection, {
|
|
6
|
-
onChange: () => null,
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
describe('LanguageSelection', () => {
|
|
10
|
-
it('has placeholder text', () => {
|
|
11
|
-
const { view } = renderWrapper();
|
|
12
|
-
view.getByText('Select your language');
|
|
13
|
-
});
|
|
14
|
-
});
|