-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathCodeEditor.stories.js
More file actions
112 lines (106 loc) · 3.1 KB
/
CodeEditor.stories.js
File metadata and controls
112 lines (106 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* eslint-disable import/no-extraneous-dependencies */
import { userEvent, expect, within } from "@storybook/test";
import "../bootstrap";
import CodeEditor from "../components/inspector/code-editor.vue";
export default {
title: "Components/CodeEditor",
component: CodeEditor,
tags: ["autodocs"],
argTypes: {
value: {
control: { type: 'text' },
description: 'The code value to display in the editor'
},
helper: {
control: { type: 'text' },
description: 'Helper text displayed below the editor'
},
dataFeature: {
control: { type: 'text' },
description: 'Data test attribute prefix for testing'
}
},
render: (args, { argTypes }) => ({
props: Object.keys(argTypes),
components: { CodeEditor },
template: '<code-editor v-bind="$props" v-model="inputValue" @input="handleInput" />',
data() {
return { inputValue: args.value };
},
methods: {
handleInput(value) {
this.inputValue = value;
}
},
watch: {
// Updates the value when the property changes in storybook controls
value(value) {
this.inputValue = value;
}
}
})
};
/**
* Stories of the component
*/
// Preview the component with basic JavaScript code
export const Preview = {
args: {
label: "Click Handler",
helper: "Enter your JavaScript code here",
dataFeature: "code-editor",
value: "console.log('Hello, World!');\n\nfunction greet(name) {\n return `Hello, ${name}!`;\n}"
}
};
// Story with empty value
export const EmptyEditor = {
args: {
label: "Empty Editor",
helper: "Start typing your code...",
dataFeature: "code-editor-empty",
value: ""
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await userEvent.type(canvas.getByRole('textbox'), 'console.log("Hello, World!");');
// Check if the code is displayed
expect(canvas.getByRole('textbox')).toHaveValue('console.log("Hello, World!");');
}
};
// Story with long code
export const LongCode = {
args: {
label: "Long Code Example",
helper: "This editor contains a longer piece of code",
dataFeature: "code-editor-long",
value: `// This is a longer code example
function processUserData(users) {
return users
.filter(user => user.active)
.map(user => ({
id: user.id,
name: user.name,
email: user.email,
role: user.role,
lastLogin: user.lastLogin,
permissions: user.permissions || []
}))
.sort((a, b) => a.name.localeCompare(b.name))
.reduce((acc, user) => {
if (!acc[user.role]) {
acc[user.role] = [];
}
acc[user.role].push(user);
return acc;
}, {});
}
// Example usage
const users = [
{ id: 1, name: 'Alice', email: 'alice@example.com', role: 'admin', active: true, lastLogin: new Date() },
{ id: 2, name: 'Bob', email: 'bob@example.com', role: 'user', active: true, lastLogin: new Date() },
{ id: 3, name: 'Charlie', email: 'charlie@example.com', role: 'admin', active: false, lastLogin: new Date() }
];
const processedUsers = processUserData(users);
console.log(processedUsers);`
}
};