@codecademy/codebytes 1.0.4-alpha.18ad182ab.0 → 1.0.4-alpha.41a141e2a.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.
Files changed (63) hide show
  1. package/.eslintrc.json +35 -0
  2. package/CHANGELOG.md +14 -0
  3. package/babel.config.js +22 -0
  4. package/jest.config.ts +21 -0
  5. package/package.json +3 -8
  6. package/project.json +52 -0
  7. package/{dist/MonacoEditor/colorsDark.js → src/MonacoEditor/colorsDark.ts} +16 -8
  8. package/src/MonacoEditor/index.tsx +56 -0
  9. package/src/MonacoEditor/theme.ts +65 -0
  10. package/src/MonacoEditor/types.ts +1 -0
  11. package/src/__tests__/codebyte.test.tsx +186 -0
  12. package/src/__tests__/editor.test.tsx +108 -0
  13. package/src/__tests__/helpers.test.tsx +39 -0
  14. package/src/__tests__/language-selection.test.tsx +14 -0
  15. package/src/api.ts +28 -0
  16. package/src/codeByteEditor.tsx +115 -0
  17. package/src/consts.ts +64 -0
  18. package/src/drawers.tsx +133 -0
  19. package/src/editor.tsx +162 -0
  20. package/src/helpers/index.ts +8 -0
  21. package/src/helpers/useEverInView.test.ts +29 -0
  22. package/src/helpers/useEverInView.ts +28 -0
  23. package/src/helpers/useIntersection.test.ts +87 -0
  24. package/src/helpers/useIntersection.ts +35 -0
  25. package/{dist/index.d.ts → src/index.ts} +0 -0
  26. package/src/languageSelection.tsx +26 -0
  27. package/src/libs/eventTracking.ts +18 -0
  28. package/{dist → src}/theme.d.ts +0 -0
  29. package/src/types.ts +30 -0
  30. package/tsconfig.json +27 -0
  31. package/tsconfig.spec.json +21 -0
  32. package/dist/MonacoEditor/colorsDark.d.ts +0 -32
  33. package/dist/MonacoEditor/index.d.ts +0 -8
  34. package/dist/MonacoEditor/index.js +0 -42
  35. package/dist/MonacoEditor/theme.d.ts +0 -2
  36. package/dist/MonacoEditor/theme.js +0 -123
  37. package/dist/MonacoEditor/types.d.ts +0 -1
  38. package/dist/MonacoEditor/types.js +0 -1
  39. package/dist/api.d.ts +0 -12
  40. package/dist/api.js +0 -41
  41. package/dist/codeByteEditor.d.ts +0 -4
  42. package/dist/codeByteEditor.js +0 -141
  43. package/dist/consts.d.ts +0 -23
  44. package/dist/consts.js +0 -34
  45. package/dist/drawers.d.ts +0 -6
  46. package/dist/drawers.js +0 -149
  47. package/dist/editor.d.ts +0 -15
  48. package/dist/editor.js +0 -194
  49. package/dist/helpers/index.d.ts +0 -2
  50. package/dist/helpers/index.js +0 -12
  51. package/dist/helpers/useEverInView.d.ts +0 -5
  52. package/dist/helpers/useEverInView.js +0 -45
  53. package/dist/helpers/useEverInView.test.js +0 -63
  54. package/dist/helpers/useIntersection.d.ts +0 -2
  55. package/dist/helpers/useIntersection.js +0 -42
  56. package/dist/helpers/useIntersection.test.js +0 -127
  57. package/dist/index.js +0 -3
  58. package/dist/languageSelection.d.ts +0 -6
  59. package/dist/languageSelection.js +0 -22
  60. package/dist/libs/eventTracking.d.ts +0 -1
  61. package/dist/libs/eventTracking.js +0 -11
  62. package/dist/types.d.ts +0 -22
  63. package/dist/types.js +0 -1
package/.eslintrc.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "extends": [
3
+ "plugin:@nrwl/nx/react",
4
+ "../../.eslintrc.json",
5
+ "../../eslint-nx"
6
+ ],
7
+ "ignorePatterns": ["!**/*", "dist"],
8
+ "overrides": [
9
+ {
10
+ "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
11
+ "rules": {}
12
+ },
13
+ {
14
+ "files": ["*.ts", "*.tsx"],
15
+ "rules": {}
16
+ },
17
+ {
18
+ "files": ["*.js", "*.jsx"],
19
+ "rules": {}
20
+ }
21
+ ],
22
+ "rules": {
23
+ // // These rules could be useful, but we haven"t gotten around to enabling them here
24
+ // // See WEB-2 for general tracking.
25
+ "@typescript-eslint/no-explicit-any": "off",
26
+ "@typescript-eslint/no-unsafe-call": "off",
27
+ "@typescript-eslint/no-non-null-assertion": "off",
28
+ "@typescript-eslint/no-unsafe-assignment": "off",
29
+ "@typescript-eslint/no-unsafe-member-access": "off",
30
+ "@typescript-eslint/no-unsafe-return": "off",
31
+ "@typescript-eslint/restrict-plus-operands": "off",
32
+ "@typescript-eslint/restrict-template-expressions": "off",
33
+ "import/no-extraneous-dependencies": "off"
34
+ }
35
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## [1.0.4-alpha.41a141e2a.0](https://github.com/codecademy-engineering/mono/compare/@codecademy/codebytes@1.0.3...@codecademy/codebytes@1.0.4-alpha.41a141e2a.0) (2022-11-28)
7
+
8
+ **Note:** Version bump only for package @codecademy/codebytes
9
+
10
+
11
+
12
+
13
+
14
+ See https://github.com/codecademy-engineering/mono/releases
@@ -0,0 +1,22 @@
1
+ module.exports = {
2
+ extends: '../../babel.defaults.js',
3
+ presets: [
4
+ [
5
+ '@nrwl/react/babel',
6
+ {
7
+ runtime: 'automatic',
8
+ useBuiltIns: 'entry',
9
+ },
10
+ ],
11
+ ],
12
+ plugins: [
13
+ [
14
+ '@emotion/babel-plugin',
15
+ {
16
+ sourceMap: true,
17
+ autoLabel: 'always',
18
+ labelFormat: '[local]',
19
+ },
20
+ ],
21
+ ],
22
+ };
package/jest.config.ts ADDED
@@ -0,0 +1,21 @@
1
+ /* eslint-disable */
2
+
3
+ import path from 'path';
4
+
5
+ import base from '../../jest.config.base';
6
+
7
+ export default base('codebytes', {
8
+ transform: {
9
+ '\\.(j|t)sx?$': [
10
+ 'babel-jest',
11
+ {
12
+ configFile: require.resolve(path.join(__dirname, './babel.config.js')),
13
+ cwd: __dirname,
14
+ },
15
+ ],
16
+ },
17
+ moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
18
+ moduleNameMapper: {
19
+ '@codecademy/tracking': '../tracking/src/index.ts',
20
+ },
21
+ });
package/package.json CHANGED
@@ -1,21 +1,16 @@
1
1
  {
2
2
  "name": "@codecademy/codebytes",
3
3
  "description": "Codebytes Editor",
4
- "version": "1.0.4-alpha.18ad182ab.0",
4
+ "version": "1.0.4-alpha.41a141e2a.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "dependencies": {
7
- "@codecademy/tracking": "1.0.3-alpha.18ad182ab.0",
7
+ "@codecademy/tracking": "1.0.3-alpha.41a141e2a.0",
8
8
  "@monaco-editor/react": "^4.4.5",
9
9
  "js-base64": "^3.6.0",
10
10
  "jsuri": "^1.3.1",
11
11
  "react-resize-observer": "^1.1.1"
12
12
  },
13
- "files": [
14
- "dist/**"
15
- ],
16
13
  "license": "MIT",
17
- "main": "./dist/index.js",
18
- "module": "./dist/index.js",
19
14
  "peerDependencies": {
20
15
  "@codecademy/gamut": "*",
21
16
  "@codecademy/gamut-icons": "*",
@@ -39,5 +34,5 @@
39
34
  "dist/**/[A-Z]**/[A-Z]*.js",
40
35
  "dist/**/[A-Z]**/index.js"
41
36
  ],
42
- "gitHead": "8fe77177836e97a90673fd488b974b678951d89f"
37
+ "gitHead": "67b9eea25d4713f12e4847b2ff1e62b64707f91c"
43
38
  }
package/project.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
3
+ "sourceRoot": "libs/codebytes/src",
4
+ "projectType": "library",
5
+ "tags": [],
6
+ "targets": {
7
+ "build": {
8
+ "executor": "@nrwl/web:rollup",
9
+ "outputs": ["dist/libs/codebytes"],
10
+ "options": {
11
+ "outputPath": "dist/libs/codebytes",
12
+ "tsConfig": "libs/codebytes/tsconfig.json",
13
+ "project": "libs/codebytes/package.json",
14
+ "entryFile": "libs/codebytes/src/index.ts",
15
+ "rollupConfig": "@nrwl/react/plugins/bundle-rollup",
16
+ "updateBuildableProjectDepsInPackageJson": true,
17
+ "compiler": "babel",
18
+ "format": ["esm", "cjs"],
19
+ "assets": [
20
+ {
21
+ "glob": "libs/codebytes/README.md",
22
+ "input": ".",
23
+ "output": "."
24
+ }
25
+ ]
26
+ }
27
+ },
28
+ "publish-build": {
29
+ "executor": "nx:run-commands",
30
+ "dependsOn": ["build"],
31
+ "options": {
32
+ "commands": [],
33
+ "parallel": false
34
+ }
35
+ },
36
+ "lint": {
37
+ "executor": "@nrwl/linter:eslint",
38
+ "options": {
39
+ "lintFilePatterns": ["libs/codebytes/**/*.{ts,tsx,js,jsx}"]
40
+ }
41
+ },
42
+ "test": {
43
+ "executor": "@nrwl/jest:jest",
44
+ "outputs": ["{workspaceRoot}/coverage/libs/codebytes"],
45
+ "options": {
46
+ "jestConfig": "libs/codebytes/jest.config.ts",
47
+ "passWithNoTests": true
48
+ },
49
+ "dependsOn": ["^build"]
50
+ }
51
+ }
52
+ }
@@ -2,8 +2,10 @@
2
2
  // This file is part of the Codebytes MVP and only includes basic configuration around theming for the SimpleMonacoEditor component
3
3
  // We are working on a monaco package in client-modules that has more configuration around themes and languages
4
4
  // Monaco as a shared package RFC https://www.notion.so/codecademy/Monaco-editor-as-a-shared-package-1f4484db165b4abc8394c3556451ef6a
5
+
5
6
  import { colors, editorColors } from '@codecademy/gamut-styles';
6
- var darkTheme = {
7
+
8
+ const darkTheme = {
7
9
  blue: editorColors.blue,
8
10
  deepPurple: editorColors.deepPurple,
9
11
  gray: editorColors.gray,
@@ -12,9 +14,10 @@ var darkTheme = {
12
14
  purple: editorColors.purple,
13
15
  red: editorColors.red,
14
16
  white: colors.white,
15
- yellow: editorColors.yellow
17
+ yellow: editorColors.yellow,
16
18
  };
17
- export var syntax = {
19
+
20
+ export const syntax = {
18
21
  attribute: darkTheme.green,
19
22
  annotation: darkTheme.red,
20
23
  atom: darkTheme.deepPurple,
@@ -34,13 +37,18 @@ export var syntax = {
34
37
  tag: darkTheme.red,
35
38
  text: darkTheme.orange,
36
39
  value: darkTheme.yellow,
37
- variable: darkTheme.green
40
+ variable: darkTheme.green,
38
41
  };
39
- export var ui = {
42
+
43
+ export const ui = {
40
44
  background: '#211E2F',
41
45
  text: darkTheme.white,
42
46
  indent: {
43
47
  active: '#393b41',
44
- inactive: '#494b51'
45
- }
46
- };
48
+ inactive: '#494b51',
49
+ },
50
+ };
51
+
52
+ export type SyntaxColors = typeof syntax;
53
+
54
+ export type UIColors = typeof ui;
@@ -0,0 +1,56 @@
1
+ // DO NOT CHANGE ANYTHING HERE
2
+ // This component is part of the Codebytes MVP and only includes basic configuration around theming
3
+ // We are working on a monaco package in client-modules that has more configuration around themes and languages
4
+ // Monaco as a shared package RFC https://www.notion.so/codecademy/Monaco-editor-as-a-shared-package-1f4484db165b4abc8394c3556451ef6a
5
+
6
+ import ReactMonacoEditor, { EditorProps, OnMount } from '@monaco-editor/react';
7
+ import { editor } from 'monaco-editor/esm/vs/editor/editor.api';
8
+ import React, { useCallback, useRef } from 'react';
9
+ import ResizeObserver from 'react-resize-observer';
10
+
11
+ import { dark } from './theme';
12
+ import { Monaco } from './types';
13
+
14
+ export type SimpleMonacoEditorProps = {
15
+ value: string;
16
+ language: string;
17
+ onChange?: EditorProps['onChange'];
18
+ };
19
+
20
+ type ThemedEditor = Parameters<OnMount>[0];
21
+
22
+ export const SimpleMonacoEditor: React.FC<SimpleMonacoEditorProps> = ({
23
+ value,
24
+ language,
25
+ onChange,
26
+ }) => {
27
+ const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
28
+ const editorWillMount = useCallback(
29
+ (editor: ThemedEditor, monaco: Monaco) => {
30
+ editorRef.current = editor;
31
+ monaco.editor.defineTheme('dark', dark);
32
+ monaco.editor.setTheme('dark');
33
+ },
34
+ []
35
+ );
36
+ return (
37
+ <>
38
+ <ResizeObserver
39
+ onResize={({ height, width }) => {
40
+ editorRef.current?.layout({
41
+ height,
42
+ width,
43
+ });
44
+ }}
45
+ />
46
+ <ReactMonacoEditor
47
+ onMount={editorWillMount}
48
+ onChange={onChange}
49
+ options={{ minimap: { enabled: false } }}
50
+ theme="vs-dark"
51
+ value={value}
52
+ language={language}
53
+ />
54
+ </>
55
+ );
56
+ };
@@ -0,0 +1,65 @@
1
+ // DO NOT CHANGE ANYTHING HERE
2
+ // This file is part of the Codebytes MVP and only includes basic configuration around theming for the SimpleMonacoEditor component
3
+ // We are working on a monaco package in client-modules that has more configuration around themes and languages
4
+ // Monaco as a shared package RFC https://www.notion.so/codecademy/Monaco-editor-as-a-shared-package-1f4484db165b4abc8394c3556451ef6a
5
+
6
+ import type * as monaco from 'monaco-editor';
7
+
8
+ import * as darkColors from './colorsDark';
9
+
10
+ const c = (color: string) => color.substr(1);
11
+
12
+ const theme = ({
13
+ ui,
14
+ syntax,
15
+ }: {
16
+ ui: darkColors.UIColors;
17
+ syntax: darkColors.SyntaxColors;
18
+ }): monaco.editor.IStandaloneThemeData => ({
19
+ base: 'vs-dark',
20
+ inherit: true,
21
+ rules: [
22
+ // Base
23
+ { token: '', foreground: c(syntax.basic) },
24
+ { token: 'regexp', foreground: c(syntax.regexp) },
25
+ { token: 'annotation', foreground: c(syntax.annotation) },
26
+ { token: 'type', foreground: c(syntax.annotation) },
27
+ { token: 'doctype', foreground: c(syntax.comment) },
28
+ { token: 'delimiter', foreground: c(syntax.decoration) },
29
+ { token: 'invalid', foreground: c(syntax.invalid) },
30
+ { token: 'emphasis', fontStyle: 'italic' },
31
+ { token: 'strong', fontStyle: 'bold' },
32
+ { token: 'variable', foreground: c(syntax.variable) },
33
+ { token: 'variable.predefined', foreground: c(syntax.variable) },
34
+ { token: 'constant', foreground: c(syntax.constant) },
35
+ { token: 'comment', foreground: c(syntax.comment) },
36
+ { token: 'number', foreground: c(syntax.number) },
37
+ { token: 'number.hex', foreground: c(syntax.number) },
38
+ { token: 'keyword.directive', foreground: c(syntax.comment) },
39
+ { token: 'include', foreground: c(syntax.comment) },
40
+ { token: 'key', foreground: c(syntax.property) },
41
+ { token: 'attribute.name', foreground: c(syntax.attribute) },
42
+ { token: 'attribute.name-numeric', foreground: c(syntax.string) },
43
+ { token: 'attribute.value', foreground: c(syntax.property) },
44
+ { token: 'attribute.value.number', foreground: c(syntax.number) },
45
+ { token: 'string', foreground: c(syntax.string) },
46
+ { token: 'string.yaml', foreground: c(syntax.string) },
47
+ { token: 'tag', foreground: c(syntax.tag) },
48
+ { token: 'tag.id.jade', foreground: c(syntax.tag) },
49
+ { token: 'tag.class.jade', foreground: c(syntax.tag) },
50
+ { token: 'metatag', foreground: c(syntax.comment) },
51
+ { token: 'attribute.value.unit', foreground: c(syntax.string) },
52
+ { token: 'keyword', foreground: c(syntax.keyword) },
53
+ { token: 'keyword.flow', foreground: c(syntax.keyword) },
54
+ ],
55
+ colors: {
56
+ 'editor.background': ui.background,
57
+ 'editor.foreground': ui.text,
58
+ 'editorIndentGuide.background': ui.indent.inactive,
59
+ 'editorIndentGuide.activeBackground': ui.indent.active,
60
+ 'editorWhitespace.foreground': syntax.comment,
61
+ 'editorLineNumber.foreground': '#9FA2AC',
62
+ },
63
+ });
64
+
65
+ export const dark = theme(darkColors);
@@ -0,0 +1 @@
1
+ export type Monaco = typeof import('monaco-editor');
@@ -0,0 +1,186 @@
1
+ import { setupRtl } from '@codecademy/gamut-tests';
2
+ import { act } from '@testing-library/react';
3
+ import userEvent from '@testing-library/user-event';
4
+ import React from 'react';
5
+
6
+ import { CodeByteEditor } from '..';
7
+ import { helloWorld, validLanguages } from '../consts';
8
+ import { trackClick } from '../helpers';
9
+ import { trackUserImpression } from '../libs/eventTracking';
10
+
11
+ const mockEditorTestId = 'mock-editor-test-id';
12
+
13
+ jest.mock('react-resize-observer');
14
+ jest.mock('../libs/eventTracking');
15
+ jest.mock('../helpers', () => ({
16
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
17
+ // @ts-ignore
18
+ ...jest.requireActual('../helpers'),
19
+ trackClick: jest.fn(),
20
+ }));
21
+ jest.mock('../helpers/useEverInView', () => ({
22
+ useEverInView: () => ({
23
+ everInView: true,
24
+ ref: () => null,
25
+ }),
26
+ }));
27
+
28
+ // This is a super simplified mock capable of render value and trigger onChange.
29
+ jest.mock('../MonacoEditor', () => ({
30
+ SimpleMonacoEditor: ({
31
+ value,
32
+ onChange,
33
+ }: {
34
+ value: string;
35
+ onChange?: (value: string) => void;
36
+ }) => (
37
+ <>
38
+ {value}
39
+ <input
40
+ data-testid={mockEditorTestId}
41
+ type="text"
42
+ onChange={(e) => {
43
+ onChange?.(e.target.value);
44
+ }}
45
+ value={value}
46
+ />
47
+ </>
48
+ ),
49
+ }));
50
+
51
+ const renderWrapper = setupRtl(CodeByteEditor, {});
52
+
53
+ describe('CodeBytes', () => {
54
+ const initialUrl = window.location.href;
55
+
56
+ afterEach(() => {
57
+ window.history.replaceState(null, '', initialUrl);
58
+ (trackClick as any).mockReset();
59
+ (trackUserImpression as any).mockReset();
60
+ });
61
+
62
+ it('has a language-specific "hello world" program defined for each language', () => {
63
+ validLanguages.forEach((language) => {
64
+ expect(helloWorld[language]).toBeDefined();
65
+ });
66
+ });
67
+
68
+ it('initializes with a language-specific "hello world" program when there is no language prop', async () => {
69
+ const { view } = renderWrapper();
70
+ const selectedLanguage = view.getByRole('combobox') as Element;
71
+ await act(async () => {
72
+ userEvent.selectOptions(selectedLanguage, ['javascript']);
73
+ });
74
+ view.getByText(helloWorld.javascript);
75
+ });
76
+
77
+ it('initializes with a language-specific "hello world" program when there is a language prop but no text prop', () => {
78
+ const { view } = renderWrapper({ language: 'javascript' });
79
+ view.getByText(helloWorld.javascript);
80
+ });
81
+
82
+ it('initializes with deserialized text when there is a text prop but no language prop', async () => {
83
+ const testString = 'yes hello';
84
+ const { view } = renderWrapper({ text: testString });
85
+ const selectedLanguage = view.getByRole('combobox') as Element;
86
+ await act(async () => {
87
+ userEvent.selectOptions(selectedLanguage, ['javascript']);
88
+ });
89
+ view.getByText(testString);
90
+ });
91
+
92
+ it('initializes with deserialized text when there is both a language and text prop', () => {
93
+ const testString = 'yes hello';
94
+ const { view } = renderWrapper({
95
+ text: testString,
96
+ language: 'javascript',
97
+ });
98
+ view.getByText(testString);
99
+ });
100
+
101
+ describe('Change Handlers', () => {
102
+ it('triggers onEdit on text edit', async () => {
103
+ const onEdit = jest.fn();
104
+ const { view } = renderWrapper({
105
+ text: '',
106
+ language: 'javascript',
107
+ onEdit,
108
+ });
109
+
110
+ const editor = view.getByTestId(mockEditorTestId);
111
+ await act(async () => {
112
+ userEvent.type(editor, 'dog');
113
+ });
114
+ expect(onEdit).toHaveBeenCalledTimes(3);
115
+ expect(onEdit).toHaveBeenLastCalledWith('g', 'javascript');
116
+ });
117
+
118
+ it('triggers onLanguageChange on language selection', async () => {
119
+ const onLanguageChange = jest.fn();
120
+ const { view } = renderWrapper({
121
+ onLanguageChange,
122
+ });
123
+
124
+ const selectedLanguage = view.getByRole('combobox') as Element;
125
+ await act(async () => {
126
+ userEvent.selectOptions(selectedLanguage, ['javascript']);
127
+ });
128
+ expect(onLanguageChange).toHaveBeenCalledWith(
129
+ "console.log('Hello world!');",
130
+ 'javascript'
131
+ );
132
+ });
133
+ });
134
+
135
+ describe('Tracking', () => {
136
+ it('triggers trackClick on clicking the logo', async () => {
137
+ const { view } = renderWrapper({});
138
+ const logo = view.getByLabelText('visit codecademy.com');
139
+ await act(async () => {
140
+ userEvent.click(logo);
141
+ });
142
+ expect(trackClick).toHaveBeenCalledWith('logo', undefined);
143
+ });
144
+
145
+ it('triggers trackClick on language selection', async () => {
146
+ const { view } = renderWrapper();
147
+ const selectedLanguage = view.getByRole('combobox') as Element;
148
+ await act(async () => {
149
+ userEvent.selectOptions(selectedLanguage, ['javascript']);
150
+ });
151
+ expect(trackClick).toHaveBeenCalledWith('lang_select', undefined);
152
+ });
153
+
154
+ it('triggers trackClick for the first edit', async () => {
155
+ const testString = 'original-value';
156
+ const { view } = renderWrapper({
157
+ text: testString,
158
+ language: 'javascript',
159
+ trackFirstEdit: true,
160
+ });
161
+
162
+ const editor = view.getByTestId(mockEditorTestId);
163
+ await act(async () => {
164
+ userEvent.type(editor, 'd');
165
+ });
166
+ expect(trackClick).toHaveBeenCalledWith('edit', undefined);
167
+ });
168
+
169
+ it('triggers trackUserImpression', () => {
170
+ renderWrapper({
171
+ text: 'some-value',
172
+ language: 'javascript',
173
+ trackingData: {
174
+ page_name: 'forum_compose',
175
+ context: 'https://discuss.codecademy.com/some-interesting/post',
176
+ },
177
+ });
178
+
179
+ expect(trackUserImpression).toHaveBeenCalledWith({
180
+ page_name: 'forum_compose',
181
+ context: 'https://discuss.codecademy.com/some-interesting/post',
182
+ target: 'codebyte',
183
+ });
184
+ });
185
+ });
186
+ });
@@ -0,0 +1,108 @@
1
+ import { setupRtl } from '@codecademy/gamut-tests';
2
+ import { act } from '@testing-library/react';
3
+ import userEvent from '@testing-library/user-event';
4
+
5
+ import { Editor } from '../editor';
6
+ import { trackClick } from '../helpers';
7
+
8
+ jest.mock('react-resize-observer');
9
+ jest.mock('../libs/eventTracking');
10
+ jest.mock('../helpers', () => ({
11
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
12
+ // @ts-ignore
13
+ ...jest.requireActual('../helpers'),
14
+ trackClick: jest.fn(),
15
+ }));
16
+
17
+ jest.mock('../MonacoEditor', () => ({
18
+ SimpleMonacoEditor: ({ value }: { value: string }) => value,
19
+ }));
20
+
21
+ const renderWrapper = setupRtl(Editor, {
22
+ hideCopyButton: false,
23
+ language: 'javascript',
24
+ text: 'hello world',
25
+ onChange: jest.fn(),
26
+ snippetsBaseUrl: '',
27
+ });
28
+
29
+ Object.defineProperty(navigator, 'clipboard', {
30
+ value: {
31
+ writeText: jest.fn().mockImplementation(() => Promise.resolve()),
32
+ },
33
+ });
34
+
35
+ describe('Editor', () => {
36
+ (global as any).fetch = jest.fn();
37
+ afterEach(() => {
38
+ (global as any).fetch.mockClear();
39
+ });
40
+
41
+ it('shows a prompt tooltip when the CodeByte has __not__ been copied via the button', () => {
42
+ const { view } = renderWrapper();
43
+ expect(view.queryByTestId('copy-confirmation-tooltip')).toBeFalsy();
44
+ view.getByTestId('copy-prompt-tooltip');
45
+ });
46
+
47
+ it('shows a confirmation tooltip when the CodeByte has been copied via the button', async () => {
48
+ const { view } = renderWrapper();
49
+ const copyBtn = view.getByTestId('copy-codebyte-btn');
50
+ await act(async () => {
51
+ userEvent.click(copyBtn as HTMLButtonElement);
52
+ });
53
+ expect(view.queryByTestId('copy-prompt-tooltip')).toBeFalsy();
54
+ view.getByTestId('copy-confirmation-tooltip');
55
+ });
56
+
57
+ it('hides the copy codebyte button if hideCopyButton prop is true"', () => {
58
+ const { view } = renderWrapper({
59
+ hideCopyButton: true,
60
+ });
61
+ expect(view.queryByTestId('copy-codebyte-btn')).toBeNull();
62
+ });
63
+
64
+ it('shows the copy codebyte button if hideCopyButton prop is not set', () => {
65
+ const { view } = renderWrapper();
66
+
67
+ view.getByTestId('copy-codebyte-btn');
68
+ });
69
+
70
+ describe('Tracking', () => {
71
+ it('tracks clicks on the run button', async () => {
72
+ (global as any).fetch.mockResolvedValue({
73
+ json: () =>
74
+ Promise.resolve({
75
+ stderr: [],
76
+ exit_code: 0,
77
+ stdout: '',
78
+ }),
79
+ });
80
+ const { view } = renderWrapper({
81
+ onChange: jest.fn(),
82
+ text: 'test',
83
+ language: 'javascript',
84
+ });
85
+
86
+ const runButton = view.getByText('Run');
87
+ await act(async () => {
88
+ userEvent.click(runButton);
89
+ });
90
+
91
+ expect(trackClick).toHaveBeenCalledWith('run', undefined);
92
+ });
93
+
94
+ it('tracks clicks on the copy codebyte button', async () => {
95
+ const { view } = renderWrapper({
96
+ onChange: jest.fn(),
97
+ text: 'test',
98
+ language: 'javascript',
99
+ });
100
+
101
+ const copyButton = view.getByTestId('copy-codebyte-btn');
102
+ await act(async () => {
103
+ userEvent.click(copyButton);
104
+ });
105
+ expect(trackClick).toHaveBeenCalledWith('copy', undefined);
106
+ });
107
+ });
108
+ });
@@ -0,0 +1,39 @@
1
+ import { trackClick } from '../helpers';
2
+ import { trackUserClick } from '../libs/eventTracking';
3
+
4
+ jest.mock('../libs/eventTracking');
5
+
6
+ const initialUrl = window.location.href;
7
+ const resetCodebytesParams = () =>
8
+ window.history.replaceState(null, '', initialUrl);
9
+
10
+ describe('trackClick', () => {
11
+ afterEach(() => {
12
+ resetCodebytesParams();
13
+ (trackUserClick as any).mockReset();
14
+ });
15
+
16
+ it('tracks user click when tracking data is provided', () => {
17
+ const target = 'foobar';
18
+ const trackingData = {
19
+ page_name: 'forum',
20
+ context: 'https://discuss.codecademy.com/some-interesting/post',
21
+ target,
22
+ };
23
+
24
+ trackClick(target, trackingData);
25
+ expect(trackUserClick).toHaveBeenCalledWith({
26
+ page_name: 'forum',
27
+ context: 'https://discuss.codecademy.com/some-interesting/post',
28
+ target,
29
+ });
30
+ });
31
+
32
+ it('tracks user click when tracking data is not provided', () => {
33
+ const target = 'foobar';
34
+ trackClick(target);
35
+ expect(trackUserClick).toHaveBeenCalledWith({
36
+ target,
37
+ });
38
+ });
39
+ });