@canva/cli 0.0.1-beta.1 → 0.0.1-beta.11

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 (79) hide show
  1. package/README.md +184 -108
  2. package/cli.js +446 -359
  3. package/lib/cjs/index.cjs +392 -0
  4. package/lib/esm/index.esm +392 -0
  5. package/package.json +11 -2
  6. package/templates/base/backend/routers/oauth.ts +393 -0
  7. package/templates/base/eslint.config.mjs +5 -277
  8. package/templates/base/package.json +26 -23
  9. package/templates/base/scripts/copy-env.ts +10 -0
  10. package/templates/base/utils/backend/bearer_middleware/bearer_middleware.ts +101 -0
  11. package/templates/base/utils/backend/bearer_middleware/index.ts +1 -0
  12. package/templates/base/utils/backend/bearer_middleware/tests/bearer_middleware.tests.ts +192 -0
  13. package/templates/base/utils/use_add_element.ts +48 -0
  14. package/templates/base/utils/use_feature_support.ts +28 -0
  15. package/templates/base/webpack.config.cjs +3 -1
  16. package/templates/common/.gitignore.template +5 -6
  17. package/templates/common/.nvmrc +1 -0
  18. package/templates/common/.prettierrc +21 -0
  19. package/templates/common/.vscode/extensions.json +6 -0
  20. package/templates/common/README.md +4 -74
  21. package/templates/common/conf/eslint-general.mjs +303 -0
  22. package/templates/common/conf/eslint-i18n.mjs +41 -0
  23. package/templates/common/conf/eslint-local-i18n-rules/index.mjs +181 -0
  24. package/templates/common/jest.config.mjs +29 -2
  25. package/templates/common/jest.setup.ts +19 -0
  26. package/templates/common/utils/backend/base_backend/create.ts +104 -0
  27. package/templates/common/utils/table_wrapper.ts +477 -0
  28. package/templates/common/utils/tests/table_wrapper.tests.ts +252 -0
  29. package/templates/common/utils/use_add_element.ts +48 -0
  30. package/templates/common/utils/use_feature_support.ts +28 -0
  31. package/templates/common/utils/use_overlay_hook.ts +74 -0
  32. package/templates/common/utils/use_selection_hook.ts +37 -0
  33. package/templates/dam/backend/server.ts +0 -7
  34. package/templates/dam/eslint.config.mjs +6 -275
  35. package/templates/dam/package.json +43 -35
  36. package/templates/dam/scripts/copy-env.ts +10 -0
  37. package/templates/dam/src/app.tsx +2 -135
  38. package/templates/dam/webpack.config.cjs +3 -1
  39. package/templates/gen_ai/README.md +1 -1
  40. package/templates/gen_ai/backend/routers/image.ts +3 -3
  41. package/templates/gen_ai/backend/server.ts +0 -7
  42. package/templates/gen_ai/eslint.config.mjs +5 -277
  43. package/templates/gen_ai/package.json +46 -38
  44. package/templates/gen_ai/scripts/copy-env.ts +10 -0
  45. package/templates/gen_ai/src/api/api.ts +1 -39
  46. package/templates/gen_ai/src/app.tsx +16 -10
  47. package/templates/gen_ai/src/components/footer.messages.ts +0 -5
  48. package/templates/gen_ai/src/components/footer.tsx +2 -16
  49. package/templates/gen_ai/src/components/image_grid.tsx +9 -7
  50. package/templates/gen_ai/src/components/index.ts +0 -1
  51. package/templates/gen_ai/src/components/prompt_input.tsx +2 -0
  52. package/templates/gen_ai/src/components/tests/remaining_credit.tests.tsx +43 -0
  53. package/templates/gen_ai/src/context/app_context.tsx +4 -26
  54. package/templates/gen_ai/src/context/context.messages.ts +1 -12
  55. package/templates/gen_ai/src/home.tsx +13 -0
  56. package/templates/gen_ai/src/index.tsx +2 -18
  57. package/templates/gen_ai/src/routes/routes.tsx +2 -2
  58. package/templates/gen_ai/utils/backend/bearer_middleware/bearer_middleware.ts +101 -0
  59. package/templates/gen_ai/utils/backend/bearer_middleware/index.ts +1 -0
  60. package/templates/gen_ai/utils/backend/bearer_middleware/tests/bearer_middleware.tests.ts +192 -0
  61. package/templates/gen_ai/webpack.config.cjs +3 -1
  62. package/templates/hello_world/eslint.config.mjs +5 -277
  63. package/templates/hello_world/package.json +46 -28
  64. package/templates/hello_world/scripts/copy-env.ts +10 -0
  65. package/templates/hello_world/src/app.tsx +25 -3
  66. package/templates/hello_world/src/tests/__snapshots__/app.tests.tsx.snap +45 -0
  67. package/templates/hello_world/src/tests/app.tests.tsx +86 -0
  68. package/templates/hello_world/utils/use_add_element.ts +48 -0
  69. package/templates/hello_world/utils/use_feature_support.ts +28 -0
  70. package/templates/hello_world/webpack.config.cjs +3 -1
  71. package/templates/dam/backend/database/database.ts +0 -42
  72. package/templates/dam/backend/routers/auth.ts +0 -285
  73. package/templates/gen_ai/backend/routers/auth.ts +0 -285
  74. package/templates/gen_ai/src/components/logged_in_status.tsx +0 -44
  75. package/templates/gen_ai/src/services/auth.tsx +0 -31
  76. package/templates/gen_ai/src/services/index.ts +0 -1
  77. /package/templates/{gen_ai → common}/utils/backend/jwt_middleware/index.ts +0 -0
  78. /package/templates/{gen_ai → common}/utils/backend/jwt_middleware/jwt_middleware.ts +0 -0
  79. /package/templates/{gen_ai → common}/utils/backend/jwt_middleware/tests/jwt_middleware.tests.ts +0 -0
@@ -1,11 +1,9 @@
1
- import typescriptEslint from "@typescript-eslint/eslint-plugin";
2
- import jest from "eslint-plugin-jest";
3
- import react from "eslint-plugin-react";
4
- import globals from "globals";
5
1
  import path from "node:path";
6
2
  import { fileURLToPath } from "node:url";
7
3
  import js from "@eslint/js";
8
4
  import { FlatCompat } from "@eslint/eslintrc";
5
+ import general from "./conf/eslint-general.mjs";
6
+ import i18n from "./conf/eslint-i18n.mjs";
9
7
 
10
8
  const __filename = fileURLToPath(import.meta.url);
11
9
  const __dirname = path.dirname(__filename);
@@ -22,8 +20,6 @@ export default [
22
20
  "**/dist",
23
21
  "**/*.d.ts",
24
22
  "**/*.d.tsx",
25
- "**/sdk",
26
- "**/internal",
27
23
  "**/*.config.*",
28
24
  ],
29
25
  },
@@ -34,276 +30,8 @@ export default [
34
30
  "plugin:@typescript-eslint/strict",
35
31
  "plugin:@typescript-eslint/stylistic",
36
32
  "plugin:react/recommended",
37
- "plugin:jest/recommended"
33
+ "plugin:jest/recommended",
38
34
  ),
39
- {
40
- plugins: {
41
- "@typescript-eslint": typescriptEslint,
42
- jest,
43
- react,
44
- },
45
- languageOptions: {
46
- globals: {
47
- ...globals.serviceworker,
48
- ...globals.browser,
49
- },
50
- },
51
- settings: {
52
- react: {
53
- version: "detect",
54
- },
55
- },
56
- rules: {
57
- "@typescript-eslint/no-non-null-assertion": "warn",
58
- "@typescript-eslint/no-empty-function": "off",
59
- "@typescript-eslint/consistent-type-imports": "error",
60
- "@typescript-eslint/no-explicit-any": "warn",
61
- "@typescript-eslint/no-empty-interface": "warn",
62
- "@typescript-eslint/consistent-type-definitions": "off",
63
- "@typescript-eslint/explicit-member-accessibility": [
64
- "error",
65
- {
66
- accessibility: "no-public",
67
- overrides: {
68
- parameterProperties: "off",
69
- },
70
- },
71
- ],
72
- "@typescript-eslint/naming-convention": [
73
- "error",
74
- {
75
- selector: "typeLike",
76
- format: ["PascalCase"],
77
- leadingUnderscore: "allow",
78
- },
79
- ],
80
- "no-invalid-this": "off",
81
- "@typescript-eslint/no-invalid-this": "error",
82
- "@typescript-eslint/no-unused-expressions": [
83
- "error",
84
- {
85
- allowShortCircuit: true,
86
- allowTernary: true,
87
- },
88
- ],
89
- "no-unused-vars": "off",
90
- "@typescript-eslint/no-unused-vars": [
91
- "error",
92
- {
93
- vars: "all",
94
- varsIgnorePattern: "^_",
95
- args: "none",
96
- ignoreRestSiblings: true,
97
- },
98
- ],
99
- "@typescript-eslint/no-require-imports": "error",
100
- "jest/no-restricted-matchers": [
101
- "error",
102
- {
103
- toContainElement:
104
- "toContainElement is not recommended as it encourages testing the internals of the components",
105
- toContainHTML:
106
- "toContainHTML is not recommended as it encourages testing the internals of the components",
107
- toHaveAttribute:
108
- "toHaveAttribute is not recommended as it encourages testing the internals of the components",
109
- toHaveClass:
110
- "toHaveClass is not recommended as it encourages testing the internals of the components",
111
- toHaveStyle:
112
- "toHaveStyle is not recommended as it encourages testing the internals of the components",
113
- },
114
- ],
115
- "react/jsx-curly-brace-presence": [
116
- "error",
117
- {
118
- props: "never",
119
- children: "never",
120
- },
121
- ],
122
- "react/jsx-tag-spacing": [
123
- "error",
124
- {
125
- closingSlash: "never",
126
- beforeSelfClosing: "allow",
127
- afterOpening: "never",
128
- beforeClosing: "allow",
129
- },
130
- ],
131
- "react/self-closing-comp": "error",
132
- "react/no-unescaped-entities": "off",
133
- "react/jsx-uses-react": "off",
134
- "react/react-in-jsx-scope": "off",
135
- "default-case": "error",
136
- eqeqeq: [
137
- "error",
138
- "always",
139
- {
140
- null: "never",
141
- },
142
- ],
143
- "no-caller": "error",
144
- "no-console": "error",
145
- "no-eval": "error",
146
- "no-inner-declarations": "error",
147
- "no-new-wrappers": "error",
148
- "no-restricted-globals": [
149
- "error",
150
- {
151
- name: "fit",
152
- message: "Don't focus tests",
153
- },
154
- {
155
- name: "fdescribe",
156
- message: "Don't focus tests",
157
- },
158
- {
159
- name: "length",
160
- message:
161
- "Undefined length - Did you mean to use window.length instead?",
162
- },
163
- {
164
- name: "name",
165
- message: "Undefined name - Did you mean to use window.name instead?",
166
- },
167
- {
168
- name: "status",
169
- message:
170
- "Undefined status - Did you mean to use window.status instead?",
171
- },
172
- {
173
- name: "spyOn",
174
- message: "Don't use spyOn directly, use jest.spyOn",
175
- },
176
- ],
177
- "no-restricted-properties": [
178
- "error",
179
- {
180
- property: "bind",
181
- message: "Don't o.f.bind(o, ...), use () => o.f(...)",
182
- },
183
- {
184
- object: "ReactDOM",
185
- property: "findDOMNode",
186
- message: "Don't use ReactDOM.findDOMNode() as it is deprecated",
187
- },
188
- ],
189
- "no-restricted-syntax": [
190
- "error",
191
- {
192
- selector: "AccessorProperty, TSAbstractAccessorProperty",
193
- message:
194
- "Accessor property syntax is not allowed, use getter and setters.",
195
- },
196
- {
197
- selector: "PrivateIdentifier",
198
- message:
199
- "Private identifiers are not allowed, use TypeScript private fields.",
200
- },
201
- {
202
- selector:
203
- "JSXOpeningElement[name.name = /^[A-Z]/] > JSXAttribute[name.name = /-/]",
204
- message:
205
- "Passing hyphenated props to custom components is not type-safe. Prefer a camelCased equivalent if available. (See https://github.com/microsoft/TypeScript/issues/55182)",
206
- },
207
- {
208
- selector:
209
- "CallExpression[callee.object.name='window'][callee.property.name='open']",
210
- message:
211
- "Apps are currently not allowed to open popups, or new tabs via browser APIs. Please use `requestOpenExternalUrl` from `@canva/platform` to link to external URLs. To learn more, see https://www.canva.dev/docs/apps/api/platform-request-open-external-url/",
212
- },
213
- ],
214
- "no-return-await": "error",
215
- "no-throw-literal": "error",
216
- "no-undef-init": "error",
217
- "no-var": "error",
218
- "object-shorthand": "error",
219
- "prefer-const": [
220
- "error",
221
- {
222
- destructuring: "all",
223
- },
224
- ],
225
- "prefer-object-spread": "error",
226
- "prefer-rest-params": "error",
227
- "prefer-spread": "error",
228
- radix: "error",
229
- },
230
- },
231
- {
232
- files: ["**/*.tsx"],
233
- rules: {
234
- "react/no-deprecated": "error",
235
- "react/forbid-elements": [
236
- "error",
237
- {
238
- forbid: [
239
- {
240
- element: "video",
241
- message:
242
- "Don't use HTML video directly. Instead, use the App UI Kit <VideoCard /> as this respects users' auto-playing preferences",
243
- },
244
- {
245
- element: "em",
246
- message:
247
- "Don't use <em> to italicize text. Canva's UI fonts don't support italic font style.",
248
- },
249
- {
250
- element: "i",
251
- message:
252
- "Don't use <i> to italicize text. Canva's UI fonts don't support italic font style.",
253
- },
254
- {
255
- element: "iframe",
256
- message:
257
- "Canva Apps aren't allowed to contain iframes. You should either recreate the UI you want to show in the iframe in the app directly, or link to your page via a `<Link>` tag. For more info see https://www.canva.dev/docs/apps/content-security-policy/#what-is-and-isnt-allowed",
258
- },
259
- {
260
- element: "script",
261
- message:
262
- "Script tags are not allowed in Canva SDK Apps. You should import JavaScript modules instead. For more info see https://www.canva.dev/docs/apps/content-security-policy/#what-is-and-isnt-allowed",
263
- },
264
- {
265
- element: "a",
266
- message:
267
- "Don't use <a> tags. Instead, use the <Link> component from the App UI Kit, and remember to open the url via the requestOpenExternalUrl method from @canva/platform.",
268
- },
269
- {
270
- element: "img",
271
- message:
272
- "Have you considered using <ImageCard /> from the App UI Kit instead?",
273
- },
274
- {
275
- element: "embed",
276
- message:
277
- "Have you considered using <EmbedCard /> from the App UI Kit instead?",
278
- },
279
- {
280
- element: "audio",
281
- message:
282
- "Have you considered using <AudioCard /> from the App UI Kit instead?",
283
- },
284
- {
285
- element: "button",
286
- message:
287
- "Rather than using the native HTML <button> element, use the <Button> component from the App UI Kit for consistency and accessibility.",
288
- },
289
- {
290
- element: "input",
291
- message:
292
- "Wherever possible, prefer using the form inputs from the App UI Kit for consistency and accessibility (TextInput, Checkbox, FileInput, etc).",
293
- },
294
- {
295
- element: "base",
296
- message:
297
- "The <base> tag is not supported in Canva Apps. We recommend using hash-based routing. For more on what is and isn't allowed in Canva Apps see https://www.canva.dev/docs/apps/content-security-policy/#what-is-and-isnt-allowed",
298
- },
299
- {
300
- element: "link",
301
- message:
302
- "If you're trying to include a css stylesheet, we recommend importing css using React, or using embedded stylesheets. For more on what is and isn't allowed in Canva Apps see https://www.canva.dev/docs/apps/content-security-policy/#what-is-and-isnt-allowed",
303
- },
304
- ],
305
- },
306
- ],
307
- },
308
- },
35
+ ...general,
36
+ ...i18n,
309
37
  ];
@@ -3,7 +3,7 @@
3
3
  "name": "empty-template",
4
4
  "description": "An empty Canva App",
5
5
  "scripts": {
6
- "extract": "formatjs extract 'src/**/*.{ts,tsx}' --out-file dist/messages_en.json",
6
+ "extract": "formatjs extract \"src/**/*.{ts,tsx}\" --out-file dist/messages_en.json",
7
7
  "build": "webpack --config webpack.config.cjs --mode production && npm run extract",
8
8
  "format": "prettier '**/*.{css,ts,tsx}' --no-config --write",
9
9
  "format:check": "prettier '**/*.{css,ts,tsx}' --no-config --check --ignore-path",
@@ -13,61 +13,79 @@
13
13
  "lint:types": "tsc",
14
14
  "start": "ts-node ./scripts/start/start.ts",
15
15
  "test": "jest --no-cache",
16
- "test:watch": "jest --watchAll"
16
+ "test:watch": "jest --watchAll",
17
+ "test:update": "npm run test -- -u",
18
+ "postinstall": "ts-node ./scripts/copy-env.ts"
17
19
  },
18
20
  "dependencies": {
19
- "@canva/app-ui-kit": "^3.8.0",
20
- "@canva/app-i18n-kit": "^0.0.1-beta.5",
21
- "@canva/design": "^1.10.0",
22
- "@canva/error": "^1.1.0",
21
+ "@canva/app-i18n-kit": "^1.0.2",
22
+ "@canva/app-ui-kit": "^4.4.0",
23
+ "@canva/asset": "^2.1.0",
24
+ "@canva/design": "^2.3.0",
25
+ "@canva/error": "^2.1.0",
26
+ "@canva/platform": "^2.1.0",
27
+ "@canva/user": "^2.1.0",
23
28
  "react": "18.3.1",
24
29
  "react-dom": "18.3.1",
25
- "react-intl": "6.6.8"
30
+ "react-intl": "6.8.7"
26
31
  },
27
32
  "devDependencies": {
28
- "@eslint/eslintrc": "3.1.0",
29
- "@eslint/js": "9.9.0",
30
- "@formatjs/cli": "6.2.12",
31
- "@formatjs/ts-transformer": "3.13.14",
33
+ "@eslint/eslintrc": "3.2.0",
34
+ "@eslint/js": "9.16.0",
35
+ "@formatjs/cli": "6.3.14",
36
+ "@formatjs/ts-transformer": "3.13.26",
32
37
  "@ngrok/ngrok": "1.4.1",
38
+ "@pmmmwh/react-refresh-webpack-plugin": "0.5.15",
33
39
  "@svgr/webpack": "8.1.0",
34
- "@types/jest": "29.5.12",
40
+ "@testing-library/react": "16.1.0",
41
+ "@types/express": "4.17.21",
42
+ "@types/express-serve-static-core": "4.19.6",
43
+ "@types/jest": "29.5.14",
44
+ "@types/jsonwebtoken": "9.0.7",
35
45
  "@types/node": "20.10.0",
36
- "@types/node-fetch": "2.6.11",
46
+ "@types/node-fetch": "2.6.12",
37
47
  "@types/node-forge": "1.3.11",
38
48
  "@types/nodemon": "1.19.6",
39
- "@types/react": "18.3.4",
40
- "@types/react-dom": "18.3.0",
49
+ "@types/react": "18.3.12",
50
+ "@types/react-dom": "18.3.1",
41
51
  "@types/webpack-env": "1.18.5",
42
- "@typescript-eslint/eslint-plugin": "8.2.0",
43
- "@typescript-eslint/parser": "8.2.0",
52
+ "@typescript-eslint/eslint-plugin": "8.18.0",
53
+ "@typescript-eslint/parser": "8.18.0",
44
54
  "chalk": "4.1.2",
45
55
  "cli-table3": "0.6.5",
46
56
  "css-loader": "7.1.2",
47
57
  "css-modules-typescript-loader": "4.0.1",
48
- "cssnano": "7.0.5",
49
- "debug": "4.3.6",
50
- "dotenv": "16.4.5",
51
- "eslint": "9.9.0",
52
- "eslint-plugin-jest": "28.8.0",
53
- "eslint-plugin-react": "7.35.0",
58
+ "cssnano": "7.0.6",
59
+ "debug": "4.4.0",
60
+ "dotenv": "16.4.7",
61
+ "eslint": "9.16.0",
62
+ "eslint-plugin-formatjs": "5.2.8",
63
+ "eslint-plugin-jest": "28.9.0",
64
+ "eslint-plugin-react": "7.37.2",
65
+ "express": "4.21.2",
66
+ "express-basic-auth": "1.2.1",
54
67
  "jest": "29.7.0",
55
- "mini-css-extract-plugin": "2.9.1",
68
+ "jest-css-modules-transform": "4.4.2",
69
+ "jest-environment-jsdom": "29.7.0",
70
+ "jsonwebtoken": "9.0.2",
71
+ "jwks-rsa": "3.1.0",
72
+ "mini-css-extract-plugin": "2.9.2",
56
73
  "node-fetch": "3.3.2",
57
74
  "node-forge": "1.3.1",
58
75
  "nodemon": "3.0.1",
59
76
  "postcss-loader": "8.1.1",
60
- "prettier": "3.3.3",
77
+ "prettier": "3.4.2",
78
+ "react-refresh": "0.16.0",
61
79
  "style-loader": "4.0.0",
62
80
  "terser-webpack-plugin": "5.3.10",
63
- "ts-jest": "29.2.4",
81
+ "ts-jest": "29.2.5",
64
82
  "ts-loader": "9.5.1",
65
83
  "ts-node": "10.9.2",
66
84
  "typescript": "5.5.4",
67
85
  "url-loader": "4.1.1",
68
- "webpack": "5.94.0",
86
+ "webpack": "5.97.1",
69
87
  "webpack-cli": "5.1.4",
70
- "webpack-dev-server": "5.0.4",
88
+ "webpack-dev-server": "5.1.0",
71
89
  "yargs": "17.7.2"
72
90
  }
73
91
  }
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env node
2
+ import * as fs from "fs";
3
+ import * as path from "path";
4
+
5
+ const envPath = path.resolve(__dirname, "..", ".env");
6
+ const templatePath = path.resolve(__dirname, "..", ".env.template");
7
+
8
+ if (!fs.existsSync(envPath)) {
9
+ fs.copyFileSync(templatePath, envPath);
10
+ }
@@ -1,16 +1,31 @@
1
1
  import { Button, Rows, Text } from "@canva/app-ui-kit";
2
- import { addNativeElement } from "@canva/design";
2
+ import { requestOpenExternalUrl } from "@canva/platform";
3
3
  import { FormattedMessage, useIntl } from "react-intl";
4
4
  import * as styles from "styles/components.css";
5
+ import { useAddElement } from "utils/use_add_element";
6
+
7
+ export const DOCS_URL = "https://www.canva.dev/docs/apps/";
5
8
 
6
9
  export const App = () => {
10
+ const addElement = useAddElement();
11
+
7
12
  const onClick = () => {
8
- addNativeElement({
9
- type: "TEXT",
13
+ addElement({
14
+ type: "text",
10
15
  children: ["Hello world!"],
11
16
  });
12
17
  };
13
18
 
19
+ const openExternalUrl = async (url: string) => {
20
+ const response = await requestOpenExternalUrl({
21
+ url,
22
+ });
23
+
24
+ if (response.status === "aborted") {
25
+ // user decided not to navigate to the link
26
+ }
27
+ };
28
+
14
29
  const intl = useIntl();
15
30
 
16
31
  return (
@@ -35,6 +50,13 @@ export const App = () => {
35
50
  "Button text to do something cool. Creates a new text element when pressed.",
36
51
  })}
37
52
  </Button>
53
+ <Button variant="secondary" onClick={() => openExternalUrl(DOCS_URL)}>
54
+ {intl.formatMessage({
55
+ defaultMessage: "Open Canva Apps SDK docs",
56
+ description:
57
+ "Button text to open Canva Apps SDK docs. Opens an external URL when pressed.",
58
+ })}
59
+ </Button>
38
60
  </Rows>
39
61
  </div>
40
62
  );
@@ -0,0 +1,45 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`Hello World Tests should have a consistent snapshot 1`] = `
4
+ <div>
5
+ <div
6
+ class="scrollContainer"
7
+ >
8
+ <div
9
+ class="jv_R6g"
10
+ style="--NZu1Zw: 16px;"
11
+ >
12
+ <p
13
+ class="fFOiLQ _5Ob_nQ fM_HdA"
14
+ >
15
+ To make changes to this app, edit the
16
+ <code>
17
+ src/app.tsx
18
+ </code>
19
+ file, then close and reopen the app in the editor to preview the changes.
20
+ </p>
21
+ <button
22
+ class="_1QoxDw Qkd66A tYI0Vw o4TrkA zKTE_w Qkd66A tYI0Vw lsXp_w ubW6qw cwOZMg zQlusQ uRvRjQ"
23
+ type="button"
24
+ >
25
+ <span
26
+ class="_38oWvQ"
27
+ >
28
+ Do something cool
29
+ </span>
30
+ </button>
31
+ <button
32
+ class="_1QoxDw Qkd66A tYI0Vw o4TrkA NT2yCg Qkd66A tYI0Vw lsXp_w cwOZMg zQlusQ uRvRjQ"
33
+ type="button"
34
+ >
35
+ <span
36
+ class="_38oWvQ"
37
+ >
38
+ Open Canva Apps SDK docs
39
+ </span>
40
+ </button>
41
+ </div>
42
+ </div>
43
+ ,
44
+ </div>
45
+ `;
@@ -0,0 +1,86 @@
1
+ /* eslint-disable formatjs/no-literal-string-in-jsx */
2
+ import React from "react";
3
+ import type { RenderResult } from "@testing-library/react";
4
+ import { fireEvent, render } from "@testing-library/react";
5
+ import { TestAppI18nProvider } from "@canva/app-i18n-kit";
6
+ import { TestAppUiProvider } from "@canva/app-ui-kit";
7
+ import { requestOpenExternalUrl } from "@canva/platform";
8
+ import { useAddElement } from "utils/use_add_element";
9
+ import { App, DOCS_URL } from "../app";
10
+
11
+ function renderInTestProvider(node: React.ReactNode): RenderResult {
12
+ return render(
13
+ // In a test environment, you should wrap your apps in `TestAppI18nProvider` and `TestAppUiProvider`, rather than `AppI18nProvider` and `AppUiProvider`
14
+ <TestAppI18nProvider>
15
+ <TestAppUiProvider>{node}</TestAppUiProvider>,
16
+ </TestAppI18nProvider>,
17
+ );
18
+ }
19
+
20
+ jest.mock("utils/use_add_element");
21
+
22
+ // This test demonstrates how to test code that uses functions from the Canva Apps SDK
23
+ // For more information on testing with the Canva Apps SDK, see https://www.canva.dev/docs/apps/testing/
24
+ describe("Hello World Tests", () => {
25
+ // Mocking the useAddElement hook
26
+ const mockAddElement = jest.fn();
27
+ const mockAddUseElement = jest.mocked(useAddElement);
28
+ const mockRequestOpenExternalUrl = jest.mocked(requestOpenExternalUrl);
29
+
30
+ beforeEach(() => {
31
+ jest.resetAllMocks();
32
+ mockAddUseElement.mockReturnValue(mockAddElement);
33
+ mockRequestOpenExternalUrl.mockResolvedValue({ status: "completed" });
34
+ });
35
+
36
+ // this test uses a mock in place of the useAddElement hook
37
+ it("should add a text element when the button is clicked", () => {
38
+ // assert that the mocks are in the expected clean state
39
+ expect(mockAddUseElement).not.toHaveBeenCalled();
40
+ expect(mockAddElement).not.toHaveBeenCalled();
41
+
42
+ const result = renderInTestProvider(<App />);
43
+
44
+ // the hook should have been called in the render process but not the callback
45
+ expect(mockAddUseElement).toHaveBeenCalled();
46
+ expect(mockAddElement).not.toHaveBeenCalled();
47
+
48
+ // get a reference to the do something cool button element
49
+ const doSomethingCoolBtn = result.getByRole("button", {
50
+ name: "Do something cool",
51
+ });
52
+
53
+ // programmatically simulate clicking the button
54
+ fireEvent.click(doSomethingCoolBtn);
55
+
56
+ // we expect that addElement has been called by the button's click handler
57
+ expect(mockAddElement).toHaveBeenCalled();
58
+ });
59
+
60
+ // this test uses a mock in place of the @canva/platform requestOpenExternalUrl function
61
+ it("should call `requestOpenExternalUrl` when the button is clicked", () => {
62
+ expect(mockRequestOpenExternalUrl).not.toHaveBeenCalled();
63
+
64
+ const result = renderInTestProvider(<App />);
65
+
66
+ // get a reference to the Apps SDK button by name
67
+ const sdkButton = result.getByRole("button", {
68
+ name: "Open Canva Apps SDK docs",
69
+ });
70
+
71
+ expect(mockRequestOpenExternalUrl).not.toHaveBeenCalled();
72
+ fireEvent.click(sdkButton);
73
+ expect(mockRequestOpenExternalUrl).toHaveBeenCalled();
74
+
75
+ // assert that the requestOpenExternalUrl function was called with the expected arguments
76
+ expect(mockRequestOpenExternalUrl.mock.calls[0][0]).toEqual({
77
+ url: DOCS_URL,
78
+ });
79
+ });
80
+
81
+ // this test demonstrates the use of a snapshot test
82
+ it("should have a consistent snapshot", () => {
83
+ const result = renderInTestProvider(<App />);
84
+ expect(result.container).toMatchSnapshot();
85
+ });
86
+ });
@@ -0,0 +1,48 @@
1
+ import type {
2
+ EmbedElement,
3
+ ImageElement,
4
+ RichtextElement,
5
+ TableElement,
6
+ TextElement,
7
+ VideoElement,
8
+ } from "@canva/design";
9
+ import { addElementAtCursor, addElementAtPoint } from "@canva/design";
10
+ import { useFeatureSupport } from "./use_feature_support";
11
+ import { features } from "@canva/platform";
12
+ import { useEffect, useState } from "react";
13
+
14
+ type AddElementParams =
15
+ | ImageElement
16
+ | VideoElement
17
+ | EmbedElement
18
+ | TextElement
19
+ | RichtextElement
20
+ | TableElement;
21
+
22
+ export const useAddElement = () => {
23
+ const isSupported = useFeatureSupport();
24
+
25
+ // Store a wrapped addElement function that checks feature support
26
+ const [addElement, setAddElement] = useState(() => {
27
+ return (element: AddElementParams) => {
28
+ if (features.isSupported(addElementAtPoint)) {
29
+ return addElementAtPoint(element);
30
+ } else if (features.isSupported(addElementAtCursor)) {
31
+ return addElementAtCursor(element);
32
+ }
33
+ };
34
+ });
35
+
36
+ useEffect(() => {
37
+ const addElement = (element: AddElementParams) => {
38
+ if (isSupported(addElementAtPoint)) {
39
+ return addElementAtPoint(element);
40
+ } else if (isSupported(addElementAtCursor)) {
41
+ return addElementAtCursor(element);
42
+ }
43
+ };
44
+ setAddElement(() => addElement);
45
+ }, [isSupported]);
46
+
47
+ return addElement;
48
+ };