@codecademy/codebytes 0.4.1-alpha.f7401f.0 → 0.5.1-alpha.a075a9.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 +10 -1
- package/jest.config.js +1 -0
- package/package.json +7 -3
- package/src/__tests__/codebyte-test.tsx +197 -0
- package/src/__tests__/editor-test.tsx +113 -0
- package/src/__tests__/helpers-test.tsx +121 -0
- package/src/__tests__/language-selection-test.tsx +14 -0
- package/src/__tests__/mocks.ts +8 -0
- package/src/consts.ts +1 -1
- package/src/editor.tsx +10 -4
- package/src/helpers/index.ts +0 -2
- package/src/index.tsx +16 -7
- package/src/types.ts +5 -5
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.5.1-alpha.a075a9.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.5.0...@codecademy/codebytes@0.5.1-alpha.a075a9.0) (2022-01-31)
|
|
7
7
|
|
|
8
8
|
**Note:** Version bump only for package @codecademy/codebytes
|
|
9
9
|
|
|
@@ -11,6 +11,15 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
|
+
## [0.5.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.4.0...@codecademy/codebytes@0.5.0) (2022-01-27)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* **Codebytes:** Refactor tracking in Codebytes ([#19](https://github.com/Codecademy/client-modules/issues/19)) ([494a944](https://github.com/Codecademy/client-modules/commit/494a94441cfbc1ea563f997607821131a7f1e007))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
14
23
|
## [0.4.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.4.0) (2022-01-21)
|
|
15
24
|
|
|
16
25
|
|
package/jest.config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../../jest.config.base')('codebytes');
|
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.5.1-alpha.a075a9.0",
|
|
5
5
|
"author": "Codecademy Engineering <dev@codecademy.com>",
|
|
6
6
|
"sideEffects": [
|
|
7
7
|
"**/*.css",
|
|
@@ -43,14 +43,18 @@
|
|
|
43
43
|
},
|
|
44
44
|
"license": "MIT",
|
|
45
45
|
"devDependencies": {
|
|
46
|
+
"@codecademy/gamut-tests": "*",
|
|
46
47
|
"@emotion/jest": "^11.3.0",
|
|
47
48
|
"@testing-library/dom": "^7.31.2",
|
|
48
49
|
"@testing-library/react": "^11.0.4",
|
|
50
|
+
"@testing-library/react-hooks": "3.2.1",
|
|
51
|
+
"@testing-library/user-event": "13.1.1",
|
|
49
52
|
"@types/jsuri": "^1.3.30",
|
|
50
|
-
"@types/loadable__component": "^5.13.2"
|
|
53
|
+
"@types/loadable__component": "^5.13.2",
|
|
54
|
+
"monaco-editor-webpack-plugin": "1.9.1"
|
|
51
55
|
},
|
|
52
56
|
"publishConfig": {
|
|
53
57
|
"access": "public"
|
|
54
58
|
},
|
|
55
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "54275e958de6d8d415eb53f7dee804ee5a170e2a"
|
|
56
60
|
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import './mocks';
|
|
2
|
+
|
|
3
|
+
import { setupRtl } from '@codecademy/gamut-tests';
|
|
4
|
+
import userEvent from '@testing-library/user-event';
|
|
5
|
+
import { encode } from 'js-base64';
|
|
6
|
+
import React from 'react';
|
|
7
|
+
|
|
8
|
+
import { CodeByteEditor } from '..';
|
|
9
|
+
import { helloWorld, validLanguages } from '../consts';
|
|
10
|
+
import { trackClick } from '../helpers';
|
|
11
|
+
import { trackUserImpression } from '../libs/eventTracking';
|
|
12
|
+
import { CodeByteEditorProps } from '../types';
|
|
13
|
+
|
|
14
|
+
const mockEditorTestId = 'mock-editor-test-id';
|
|
15
|
+
|
|
16
|
+
// This is a super simplified mock capable of render value and trigger onChange.
|
|
17
|
+
jest.mock('../MonacoEditor', () => ({
|
|
18
|
+
SimpleMonacoEditor: ({
|
|
19
|
+
value,
|
|
20
|
+
onChange,
|
|
21
|
+
}: {
|
|
22
|
+
value: string;
|
|
23
|
+
onChange?: (value: string) => void;
|
|
24
|
+
}) => (
|
|
25
|
+
<>
|
|
26
|
+
{value}
|
|
27
|
+
<input
|
|
28
|
+
data-testid={mockEditorTestId}
|
|
29
|
+
type="text"
|
|
30
|
+
onChange={(e) => {
|
|
31
|
+
onChange?.(e.target.value);
|
|
32
|
+
}}
|
|
33
|
+
value={value}
|
|
34
|
+
/>
|
|
35
|
+
</>
|
|
36
|
+
),
|
|
37
|
+
}));
|
|
38
|
+
|
|
39
|
+
const renderWrapper = setupRtl(CodeByteEditor, {});
|
|
40
|
+
|
|
41
|
+
type RenderWrapperWithProps = CodeByteEditorProps & { mode?: string };
|
|
42
|
+
|
|
43
|
+
const renderWrapperWith = ({ mode, ...rest }: RenderWrapperWithProps) => {
|
|
44
|
+
const url = new URL(window.location.href);
|
|
45
|
+
|
|
46
|
+
const { text, language } = rest;
|
|
47
|
+
if (text) {
|
|
48
|
+
url.searchParams.set('text', encode(text));
|
|
49
|
+
}
|
|
50
|
+
if (language) {
|
|
51
|
+
url.searchParams.set('lang', language);
|
|
52
|
+
}
|
|
53
|
+
url.searchParams.set('client-name', 'forum');
|
|
54
|
+
url.searchParams.set(
|
|
55
|
+
'page',
|
|
56
|
+
'https://discuss.codecademy.com/some-interesting/post'
|
|
57
|
+
);
|
|
58
|
+
if (mode) {
|
|
59
|
+
url.searchParams.set('mode', mode);
|
|
60
|
+
}
|
|
61
|
+
window.history.replaceState({}, '', url.toString());
|
|
62
|
+
|
|
63
|
+
return renderWrapper(rest);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
describe('CodeBytes', () => {
|
|
67
|
+
const initialUrl = window.location.href;
|
|
68
|
+
|
|
69
|
+
afterEach(() => {
|
|
70
|
+
window.history.replaceState(null, '', initialUrl);
|
|
71
|
+
(trackClick as any).mockReset();
|
|
72
|
+
(trackUserImpression as any).mockReset();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('has a language-specific "hello world" program defined for each language', () => {
|
|
76
|
+
validLanguages.forEach((language) => {
|
|
77
|
+
expect(helloWorld[language]).toBeDefined();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('initializes with a language-specific "hello world" program when there is no language prop', () => {
|
|
82
|
+
const { view } = renderWrapper();
|
|
83
|
+
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
84
|
+
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
85
|
+
view.getByText(helloWorld.javascript);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('initializes with a language-specific "hello world" program when there is a language prop but no text prop', () => {
|
|
89
|
+
const { view } = renderWrapper({ language: 'javascript' });
|
|
90
|
+
view.getByText(helloWorld.javascript);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('initializes with deserialized text when there is a text prop but no language prop', () => {
|
|
94
|
+
const testString = 'yes hello';
|
|
95
|
+
const { view } = renderWrapper({ text: testString });
|
|
96
|
+
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
97
|
+
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
98
|
+
view.getByText(testString);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('initializes with deserialized text when there is both a language and text prop', () => {
|
|
102
|
+
const testString = 'yes hello';
|
|
103
|
+
const { view } = renderWrapper({
|
|
104
|
+
text: testString,
|
|
105
|
+
language: 'javascript',
|
|
106
|
+
});
|
|
107
|
+
view.getByText(testString);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('Change Handlers', () => {
|
|
111
|
+
it('triggers onEdit on text edit', () => {
|
|
112
|
+
const onEdit = jest.fn();
|
|
113
|
+
const { view } = renderWrapper({
|
|
114
|
+
text: '',
|
|
115
|
+
language: 'javascript',
|
|
116
|
+
onEdit,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const editor = view.getByTestId(mockEditorTestId);
|
|
120
|
+
userEvent.type(editor, 'dog');
|
|
121
|
+
|
|
122
|
+
expect(onEdit).toHaveBeenCalledTimes(3);
|
|
123
|
+
expect(onEdit).toHaveBeenLastCalledWith('dog', 'javascript');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('triggers onLanguageChange on language selection', () => {
|
|
127
|
+
const onLanguageChange = jest.fn();
|
|
128
|
+
const { view } = renderWrapper({
|
|
129
|
+
onLanguageChange,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
133
|
+
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
134
|
+
|
|
135
|
+
expect(onLanguageChange).toHaveBeenCalledWith(
|
|
136
|
+
"console.log('Hello world!');",
|
|
137
|
+
'javascript'
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe('Tracking', () => {
|
|
143
|
+
it('triggers trackClick on clicking the logo', () => {
|
|
144
|
+
const { view } = renderWrapper({});
|
|
145
|
+
const logo = view.getByLabelText('visit codecademy.com');
|
|
146
|
+
userEvent.click(logo);
|
|
147
|
+
expect(trackClick).toHaveBeenCalledWith('logo');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('triggers trackClick on language selection', () => {
|
|
151
|
+
const { view } = renderWrapper();
|
|
152
|
+
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
153
|
+
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
154
|
+
expect(trackClick).toHaveBeenCalledWith('lang_select');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('triggers trackClick for the first edit in view mode', () => {
|
|
158
|
+
const testString = 'original-value';
|
|
159
|
+
const { view } = renderWrapper({
|
|
160
|
+
text: testString,
|
|
161
|
+
language: 'javascript',
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const editor = view.getByTestId(mockEditorTestId);
|
|
165
|
+
userEvent.type(editor, 'd');
|
|
166
|
+
|
|
167
|
+
expect(trackClick).toHaveBeenCalledWith('edit');
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('triggers trackUserImpression for view mode', () => {
|
|
171
|
+
renderWrapperWith({
|
|
172
|
+
text: 'some-value',
|
|
173
|
+
language: 'javascript',
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
expect(trackUserImpression).toHaveBeenCalledWith({
|
|
177
|
+
page_name: 'forum',
|
|
178
|
+
context: 'https://discuss.codecademy.com/some-interesting/post',
|
|
179
|
+
target: 'codebyte',
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('triggers trackUserImpression for compose mode', () => {
|
|
184
|
+
renderWrapperWith({
|
|
185
|
+
text: 'some-value',
|
|
186
|
+
language: 'javascript',
|
|
187
|
+
mode: 'compose',
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
expect(trackUserImpression).toHaveBeenCalledWith({
|
|
191
|
+
page_name: 'forum_compose',
|
|
192
|
+
context: 'https://discuss.codecademy.com/some-interesting/post',
|
|
193
|
+
target: 'codebyte',
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import './mocks';
|
|
2
|
+
|
|
3
|
+
import { setupRtl } from '@codecademy/gamut-tests';
|
|
4
|
+
import { act } from '@testing-library/react';
|
|
5
|
+
import userEvent from '@testing-library/user-event';
|
|
6
|
+
import React from 'react';
|
|
7
|
+
|
|
8
|
+
import { Editor } from '../editor';
|
|
9
|
+
import { trackClick } from '../helpers';
|
|
10
|
+
|
|
11
|
+
jest.mock('../MonacoEditor', () => ({
|
|
12
|
+
SimpleMonacoEditor: ({ value }: { value: string }) => <>{value}</>,
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
const renderWrapper = setupRtl(Editor, {
|
|
16
|
+
hideCopyButton: false,
|
|
17
|
+
language: 'javascript',
|
|
18
|
+
text: 'hello world',
|
|
19
|
+
onChange: jest.fn(),
|
|
20
|
+
snippetsBaseUrl: '',
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
Object.defineProperty(navigator, 'clipboard', {
|
|
24
|
+
value: {
|
|
25
|
+
writeText: jest.fn().mockImplementation(() => Promise.resolve()),
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('Editor', () => {
|
|
30
|
+
(global as any).fetch = jest.fn();
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
(global as any).fetch.mockClear();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('shows a prompt tooltip when the CodeByte has __not__ been copied via the button', () => {
|
|
36
|
+
const { view } = renderWrapper();
|
|
37
|
+
expect(view.queryByTestId('copy-confirmation-tooltip')).toBeFalsy();
|
|
38
|
+
view.getByTestId('copy-prompt-tooltip');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('shows a confirmation tooltip when the CodeByte has been copied via the button', () => {
|
|
42
|
+
const { view } = renderWrapper();
|
|
43
|
+
const copyBtn = view.getByTestId('copy-codebyte-btn');
|
|
44
|
+
userEvent.click(copyBtn as HTMLButtonElement);
|
|
45
|
+
expect(view.queryByTestId('copy-prompt-tooltip')).toBeFalsy();
|
|
46
|
+
view.getByTestId('copy-confirmation-tooltip');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('hides the copy codebyte button if hideCopyButton prop is true"', () => {
|
|
50
|
+
const { view } = renderWrapper({
|
|
51
|
+
hideCopyButton: true,
|
|
52
|
+
});
|
|
53
|
+
expect(view.queryByTestId('copy-codebyte-btn')).toBeNull();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('shows the copy codebyte button if hideCopyButton prop is not set', () => {
|
|
57
|
+
const { view } = renderWrapper();
|
|
58
|
+
|
|
59
|
+
view.getByTestId('copy-codebyte-btn');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('Change Handlers', () => {
|
|
63
|
+
it('triggers onCopy upon clicking the copy button', () => {
|
|
64
|
+
const onCopy = jest.fn();
|
|
65
|
+
const { view } = renderWrapper({
|
|
66
|
+
onCopy,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const copyButton = view.getByTestId('copy-codebyte-btn');
|
|
70
|
+
userEvent.click(copyButton);
|
|
71
|
+
|
|
72
|
+
expect(onCopy).toHaveBeenCalled();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('Tracking', () => {
|
|
77
|
+
it('tracks clicks on the run button', async () => {
|
|
78
|
+
(global as any).fetch.mockResolvedValue({
|
|
79
|
+
json: () =>
|
|
80
|
+
Promise.resolve({
|
|
81
|
+
stderr: [],
|
|
82
|
+
exit_code: 0,
|
|
83
|
+
stdout: '',
|
|
84
|
+
}),
|
|
85
|
+
});
|
|
86
|
+
const { view } = renderWrapper({
|
|
87
|
+
onChange: jest.fn(),
|
|
88
|
+
text: 'test',
|
|
89
|
+
language: 'javascript',
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const runButton = view.getByText('Run');
|
|
93
|
+
await act(async () => {
|
|
94
|
+
userEvent.click(runButton);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
expect(trackClick).toHaveBeenCalledWith('run');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('tracks clicks on the copy codebyte button', () => {
|
|
101
|
+
const { view } = renderWrapper({
|
|
102
|
+
onChange: jest.fn(),
|
|
103
|
+
text: 'test',
|
|
104
|
+
language: 'javascript',
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const copyButton = view.getByTestId('copy-codebyte-btn');
|
|
108
|
+
userEvent.click(copyButton);
|
|
109
|
+
|
|
110
|
+
expect(trackClick).toHaveBeenCalledWith('copy');
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { encode } from 'js-base64';
|
|
2
|
+
|
|
3
|
+
import { CodebytesParams, getOptions, trackClick } from '../helpers';
|
|
4
|
+
import { trackUserClick } from '../libs/eventTracking';
|
|
5
|
+
|
|
6
|
+
jest.mock('../libs/eventTracking');
|
|
7
|
+
|
|
8
|
+
const initialUrl = window.location.href;
|
|
9
|
+
const resetCodebytesParams = () =>
|
|
10
|
+
window.history.replaceState(null, '', initialUrl);
|
|
11
|
+
type SetCodebytesParamsProps = Record<CodebytesParams, string>;
|
|
12
|
+
const setCodebytesParams = (params: SetCodebytesParamsProps) => {
|
|
13
|
+
if (params.text) {
|
|
14
|
+
params.text = encode(params.text);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const url = new URL(window.location.href);
|
|
18
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
19
|
+
url.searchParams.set(key, value);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
window.history.replaceState({}, '', url.toString());
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
describe('getOptions', () => {
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
resetCodebytesParams();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('parses props out of the window locations', () => {
|
|
31
|
+
const text = `
|
|
32
|
+
const msg = 'Sup?';
|
|
33
|
+
console.log(msg);
|
|
34
|
+
`.trim();
|
|
35
|
+
setCodebytesParams({
|
|
36
|
+
lang: 'javascript',
|
|
37
|
+
text,
|
|
38
|
+
'copy-mode': '',
|
|
39
|
+
'client-name': 'forum',
|
|
40
|
+
page: 'https://discuss.codecademy.com/some-interesting/post',
|
|
41
|
+
mode: 'compose',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(getOptions()).toEqual({
|
|
45
|
+
clientName: 'forum',
|
|
46
|
+
parentPage: 'https://discuss.codecademy.com/some-interesting/post',
|
|
47
|
+
renderMode: 'compose',
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('trackClick', () => {
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
resetCodebytesParams();
|
|
55
|
+
(trackUserClick as any).mockReset();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('proxies to trackUserClick setting the page_name, context, and target', () => {
|
|
59
|
+
const target = 'foobar';
|
|
60
|
+
setCodebytesParams({
|
|
61
|
+
lang: 'javascript',
|
|
62
|
+
text: `
|
|
63
|
+
const msg = 'Sup?';
|
|
64
|
+
console.log(msg);
|
|
65
|
+
`.trim(),
|
|
66
|
+
'copy-mode': '',
|
|
67
|
+
'client-name': 'forum',
|
|
68
|
+
page: 'https://discuss.codecademy.com/some-interesting/post',
|
|
69
|
+
mode: '',
|
|
70
|
+
});
|
|
71
|
+
trackClick(target);
|
|
72
|
+
expect(trackUserClick).toHaveBeenCalledWith({
|
|
73
|
+
page_name: 'forum',
|
|
74
|
+
context: 'https://discuss.codecademy.com/some-interesting/post',
|
|
75
|
+
target,
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('defaults the client name to "Unknown"', () => {
|
|
80
|
+
const target = 'foobar';
|
|
81
|
+
setCodebytesParams({
|
|
82
|
+
lang: 'javascript',
|
|
83
|
+
text: `
|
|
84
|
+
const msg = 'Sup?';
|
|
85
|
+
console.log(msg);
|
|
86
|
+
`.trim(),
|
|
87
|
+
'copy-mode': '',
|
|
88
|
+
'client-name': '',
|
|
89
|
+
page: 'https://discuss.codecademy.com/some-interesting/post',
|
|
90
|
+
mode: '',
|
|
91
|
+
});
|
|
92
|
+
trackClick(target);
|
|
93
|
+
expect(trackUserClick).toHaveBeenCalledWith({
|
|
94
|
+
page_name: 'Unknown',
|
|
95
|
+
context: 'https://discuss.codecademy.com/some-interesting/post',
|
|
96
|
+
target,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('embeds the render mode into the page_name', () => {
|
|
101
|
+
const mode = 'compose';
|
|
102
|
+
const target = 'foobar';
|
|
103
|
+
setCodebytesParams({
|
|
104
|
+
lang: 'javascript',
|
|
105
|
+
text: `
|
|
106
|
+
const msg = 'Sup?';
|
|
107
|
+
console.log(msg);
|
|
108
|
+
`.trim(),
|
|
109
|
+
'copy-mode': '',
|
|
110
|
+
'client-name': 'forum',
|
|
111
|
+
page: 'https://discuss.codecademy.com/some-interesting/post',
|
|
112
|
+
mode,
|
|
113
|
+
});
|
|
114
|
+
trackClick(target);
|
|
115
|
+
expect(trackUserClick).toHaveBeenCalledWith({
|
|
116
|
+
page_name: `forum_${mode}`,
|
|
117
|
+
context: 'https://discuss.codecademy.com/some-interesting/post',
|
|
118
|
+
target,
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
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('Which language do you want to code in?');
|
|
13
|
+
});
|
|
14
|
+
});
|
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 LanguageOption[];
|
|
19
|
+
) as Exclude<LanguageOption, ''>[];
|
|
20
20
|
|
|
21
21
|
const cpp = `#include <iostream>
|
|
22
22
|
int main() {
|
package/src/editor.tsx
CHANGED
|
@@ -14,6 +14,7 @@ import type { LanguageOption } from './consts';
|
|
|
14
14
|
import { Drawers } from './drawers';
|
|
15
15
|
import { trackClick } from './helpers';
|
|
16
16
|
import { SimpleMonacoEditor } from './MonacoEditor';
|
|
17
|
+
import { CodebytesChangeHandler } from './types';
|
|
17
18
|
|
|
18
19
|
const Output = styled.pre<{ hasError: boolean }>`
|
|
19
20
|
width: 100%;
|
|
@@ -40,8 +41,8 @@ type EditorProps = {
|
|
|
40
41
|
language: LanguageOption;
|
|
41
42
|
text: string;
|
|
42
43
|
onChange: (text: string) => void;
|
|
43
|
-
onCopy?: (text: string, language: string) => void;
|
|
44
44
|
snippetsBaseUrl?: string;
|
|
45
|
+
onCopy?: CodebytesChangeHandler;
|
|
45
46
|
};
|
|
46
47
|
|
|
47
48
|
export const Editor: React.FC<EditorProps> = ({
|
|
@@ -61,8 +62,8 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
61
62
|
.writeText(text)
|
|
62
63
|
// eslint-disable-next-line no-console
|
|
63
64
|
.catch(() => console.error('Failed to copy'));
|
|
64
|
-
onCopy?.(text, language);
|
|
65
65
|
setIsCodeByteCopied(true);
|
|
66
|
+
onCopy?.(text, language);
|
|
66
67
|
trackClick('copy');
|
|
67
68
|
}
|
|
68
69
|
};
|
|
@@ -133,15 +134,20 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
133
134
|
variant="secondary"
|
|
134
135
|
onClick={onCopyClick}
|
|
135
136
|
onBlur={() => setIsCodeByteCopied(false)}
|
|
137
|
+
data-testid="copy-codebyte-btn"
|
|
136
138
|
>
|
|
137
139
|
<CopyIconStyled aria-hidden="true" /> Copy Codebyte
|
|
138
140
|
</TextButton>
|
|
139
141
|
}
|
|
140
142
|
>
|
|
141
143
|
{isCodeByteCopied ? (
|
|
142
|
-
<span role="alert">
|
|
144
|
+
<span data-testid="copy-confirmation-tooltip" role="alert">
|
|
145
|
+
Copied!
|
|
146
|
+
</span>
|
|
143
147
|
) : (
|
|
144
|
-
<span
|
|
148
|
+
<span data-testid="copy-prompt-tooltip">
|
|
149
|
+
Copy to your clipboard
|
|
150
|
+
</span>
|
|
145
151
|
)}
|
|
146
152
|
</ToolTip>
|
|
147
153
|
) : null}
|
package/src/helpers/index.ts
CHANGED
package/src/index.tsx
CHANGED
|
@@ -13,7 +13,7 @@ import { trackUserImpression } from './libs/eventTracking';
|
|
|
13
13
|
import { CodeByteEditorProps } from './types';
|
|
14
14
|
|
|
15
15
|
const editorStates = states({
|
|
16
|
-
|
|
16
|
+
isIFrame: { height: '100vh' },
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
const editorBaseStyles = system.css({
|
|
@@ -37,14 +37,22 @@ const EditorContainer = styled(Background)<EditorStyleProps>(
|
|
|
37
37
|
export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
38
38
|
text: initialText,
|
|
39
39
|
language: initialLanguage,
|
|
40
|
-
hideCopyButton,
|
|
41
|
-
|
|
40
|
+
hideCopyButton = false,
|
|
41
|
+
isIFrame = false,
|
|
42
42
|
snippetsBaseUrl,
|
|
43
43
|
onEdit,
|
|
44
44
|
onLanguageChange,
|
|
45
|
+
onCopy,
|
|
45
46
|
}) => {
|
|
46
|
-
const
|
|
47
|
-
|
|
47
|
+
const getInitialText = () => {
|
|
48
|
+
if (initialText !== undefined) return initialText;
|
|
49
|
+
return initialLanguage ? helloWorld[initialLanguage] : '';
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const [text, setText] = useState<string>(getInitialText());
|
|
53
|
+
const [language, setLanguage] = useState<LanguageOption>(
|
|
54
|
+
initialLanguage ?? ''
|
|
55
|
+
);
|
|
48
56
|
const [hasBeenEdited, setHasBeenEdited] = useState(false);
|
|
49
57
|
|
|
50
58
|
useEffect(() => {
|
|
@@ -61,7 +69,7 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
61
69
|
}, []);
|
|
62
70
|
|
|
63
71
|
return (
|
|
64
|
-
<EditorContainer bg="black" as="main"
|
|
72
|
+
<EditorContainer bg="black" as="main" isIFrame={isIFrame}>
|
|
65
73
|
<Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
|
|
66
74
|
<IconButton
|
|
67
75
|
icon={FaviconIcon}
|
|
@@ -81,7 +89,6 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
81
89
|
onChange={(newText: string) => {
|
|
82
90
|
setText(newText);
|
|
83
91
|
onEdit?.(newText, language);
|
|
84
|
-
|
|
85
92
|
const { renderMode } = getOptions();
|
|
86
93
|
if (!renderMode && hasBeenEdited === false) {
|
|
87
94
|
setHasBeenEdited(true);
|
|
@@ -89,6 +96,7 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
89
96
|
}
|
|
90
97
|
}}
|
|
91
98
|
snippetsBaseUrl={snippetsBaseUrl}
|
|
99
|
+
onCopy={onCopy}
|
|
92
100
|
/>
|
|
93
101
|
) : (
|
|
94
102
|
<LanguageSelection
|
|
@@ -99,6 +107,7 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
99
107
|
setText(newText);
|
|
100
108
|
trackClick('lang_select');
|
|
101
109
|
onLanguageChange?.(newText, newLanguage);
|
|
110
|
+
trackClick('lang_select');
|
|
102
111
|
}}
|
|
103
112
|
/>
|
|
104
113
|
)}
|
package/src/types.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { LanguageOption } from './consts';
|
|
2
2
|
|
|
3
|
-
interface CodebytesChangeHandler {
|
|
3
|
+
export interface CodebytesChangeHandler {
|
|
4
4
|
(text: string, language: LanguageOption): void;
|
|
5
5
|
}
|
|
6
6
|
export interface CodeByteEditorProps {
|
|
7
|
-
text
|
|
8
|
-
language
|
|
9
|
-
hideCopyButton
|
|
7
|
+
text?: string;
|
|
8
|
+
language?: LanguageOption;
|
|
9
|
+
hideCopyButton?: boolean;
|
|
10
10
|
onCopy?: CodebytesChangeHandler;
|
|
11
|
-
|
|
11
|
+
isIFrame?: boolean;
|
|
12
12
|
snippetsBaseUrl?: string;
|
|
13
13
|
onEdit?: CodebytesChangeHandler;
|
|
14
14
|
onLanguageChange?: CodebytesChangeHandler;
|