nebula-cms 0.1.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/.claude/settings.local.json +42 -0
- package/.github/workflows/ci.yml +31 -0
- package/.mcp.json +12 -0
- package/.prettierignore +5 -0
- package/.prettierrc.cjs +22 -0
- package/AGENTS.md +183 -0
- package/LICENSE +201 -0
- package/README.md +128 -0
- package/package.json +74 -0
- package/playground/.claude/settings.local.json +5 -0
- package/playground/astro.config.mjs +7 -0
- package/playground/node_modules/.bin/astro +21 -0
- package/playground/node_modules/.bin/rollup +21 -0
- package/playground/node_modules/.bin/tsc +21 -0
- package/playground/node_modules/.bin/tsserver +21 -0
- package/playground/node_modules/.bin/vite +21 -0
- package/playground/node_modules/.vite/_svelte_metadata.json +1 -0
- package/playground/node_modules/.vite/deps/@astrojs_svelte_client__js.js +80 -0
- package/playground/node_modules/.vite/deps/@astrojs_svelte_client__js.js.map +7 -0
- package/playground/node_modules/.vite/deps/_metadata.json +184 -0
- package/playground/node_modules/.vite/deps/astro___aria-query.js +6776 -0
- package/playground/node_modules/.vite/deps/astro___aria-query.js.map +7 -0
- package/playground/node_modules/.vite/deps/astro___axobject-query.js +3754 -0
- package/playground/node_modules/.vite/deps/astro___axobject-query.js.map +7 -0
- package/playground/node_modules/.vite/deps/astro___html-escaper.js +34 -0
- package/playground/node_modules/.vite/deps/astro___html-escaper.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-AJXJMYAF.js +0 -0
- package/playground/node_modules/.vite/deps/chunk-AJXJMYAF.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-ALJIOON6.js +1005 -0
- package/playground/node_modules/.vite/deps/chunk-ALJIOON6.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-BUSYA2B4.js +8 -0
- package/playground/node_modules/.vite/deps/chunk-BUSYA2B4.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-CNYJBM5F.js +21 -0
- package/playground/node_modules/.vite/deps/chunk-CNYJBM5F.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-DBPNBGEI.js +223 -0
- package/playground/node_modules/.vite/deps/chunk-DBPNBGEI.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-G3C2FXJT.js +204 -0
- package/playground/node_modules/.vite/deps/chunk-G3C2FXJT.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-GKDKFWC5.js +27 -0
- package/playground/node_modules/.vite/deps/chunk-GKDKFWC5.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-HNCLEOC5.js +4376 -0
- package/playground/node_modules/.vite/deps/chunk-HNCLEOC5.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-JICYXBFU.js +688 -0
- package/playground/node_modules/.vite/deps/chunk-JICYXBFU.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-KCUTL6DD.js +5099 -0
- package/playground/node_modules/.vite/deps/chunk-KCUTL6DD.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-ZP4UNCSN.js +23 -0
- package/playground/node_modules/.vite/deps/chunk-ZP4UNCSN.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-ZREFNRZZ.js +148 -0
- package/playground/node_modules/.vite/deps/chunk-ZREFNRZZ.js.map +7 -0
- package/playground/node_modules/.vite/deps/package.json +3 -0
- package/playground/node_modules/.vite/deps/smol-toml.js +843 -0
- package/playground/node_modules/.vite/deps/smol-toml.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte.js +55 -0
- package/playground/node_modules/.vite/deps/svelte.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte___clsx.js +9 -0
- package/playground/node_modules/.vite/deps/svelte___clsx.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_animate.js +57 -0
- package/playground/node_modules/.vite/deps/svelte_animate.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_attachments.js +15 -0
- package/playground/node_modules/.vite/deps/svelte_attachments.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_easing.js +67 -0
- package/playground/node_modules/.vite/deps/svelte_easing.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_events.js +11 -0
- package/playground/node_modules/.vite/deps/svelte_events.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal.js +5 -0
- package/playground/node_modules/.vite/deps/svelte_internal.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_client.js +402 -0
- package/playground/node_modules/.vite/deps/svelte_internal_client.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_disclose-version.js +10 -0
- package/playground/node_modules/.vite/deps/svelte_internal_disclose-version.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_async.js +8 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_async.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_legacy.js +8 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_legacy.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_tracing.js +8 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_tracing.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_legacy.js +35 -0
- package/playground/node_modules/.vite/deps/svelte_legacy.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_motion.js +545 -0
- package/playground/node_modules/.vite/deps/svelte_motion.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity.js +29 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity_window.js +127 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity_window.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_store.js +103 -0
- package/playground/node_modules/.vite/deps/svelte_store.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_transition.js +208 -0
- package/playground/node_modules/.vite/deps/svelte_transition.js.map +7 -0
- package/playground/package.json +16 -0
- package/playground/pnpm-lock.yaml +3167 -0
- package/playground/src/content/authors/jane-doe.json +8 -0
- package/playground/src/content/config/build.toml +2 -0
- package/playground/src/content/courses/web-fundamentals.json +29 -0
- package/playground/src/content/docs/advanced.mdx +6 -0
- package/playground/src/content/docs/intro.md +6 -0
- package/playground/src/content/guides/getting-started.mdx +6 -0
- package/playground/src/content/posts/hello-world.md +7 -0
- package/playground/src/content/products/t-shirt.json +16 -0
- package/playground/src/content/recipes/pancakes.mdoc +8 -0
- package/playground/src/content/settings/site.yml +2 -0
- package/playground/src/content.config.ts +198 -0
- package/playground/src/env.d.ts +1 -0
- package/playground/src/pages/index.astro +11 -0
- package/playground/src/pages/nebula.astro +14 -0
- package/pnpm-workspace.yaml +2 -0
- package/scripts/subset-icons.mjs +178 -0
- package/src/astro/index.ts +295 -0
- package/src/client/Admin.svelte +283 -0
- package/src/client/components/BackendPicker.svelte +291 -0
- package/src/client/components/DraftChip.svelte +46 -0
- package/src/client/components/MetadataForm.svelte +56 -0
- package/src/client/components/ThemeToggle.svelte +18 -0
- package/src/client/components/dialogs/DeleteDraftDialog.svelte +51 -0
- package/src/client/components/dialogs/FilenameDialog.svelte +129 -0
- package/src/client/components/editor/EditorPane.svelte +227 -0
- package/src/client/components/editor/EditorTabs.svelte +81 -0
- package/src/client/components/editor/EditorToolbar.svelte +131 -0
- package/src/client/components/editor/FormatSelector.svelte +66 -0
- package/src/client/components/editor/Toolbar.svelte +17 -0
- package/src/client/components/fields/ArrayField.svelte +339 -0
- package/src/client/components/fields/ArrayItem.svelte +325 -0
- package/src/client/components/fields/BooleanField.svelte +114 -0
- package/src/client/components/fields/DateField.svelte +82 -0
- package/src/client/components/fields/EnumField.svelte +74 -0
- package/src/client/components/fields/FieldWrapper.svelte +96 -0
- package/src/client/components/fields/NumberField.svelte +99 -0
- package/src/client/components/fields/ObjectField.svelte +121 -0
- package/src/client/components/fields/SchemaField.svelte +107 -0
- package/src/client/components/fields/StringField.svelte +104 -0
- package/src/client/components/sidebar/AdminSidebar.svelte +339 -0
- package/src/client/components/sidebar/AdminSidebarSort.svelte +123 -0
- package/src/client/css/a11y.css +14 -0
- package/src/client/css/btn.css +113 -0
- package/src/client/css/dialog.css +29 -0
- package/src/client/css/field-input.css +39 -0
- package/src/client/css/reset.css +59 -0
- package/src/client/css/theme.css +77 -0
- package/src/client/index.ts +1 -0
- package/src/client/js/drafts/merge.svelte.ts +121 -0
- package/src/client/js/drafts/ops.svelte.ts +227 -0
- package/src/client/js/drafts/storage.ts +108 -0
- package/src/client/js/drafts/workers/diff.ts +40 -0
- package/src/client/js/editor/editor.svelte.ts +343 -0
- package/src/client/js/editor/languages.ts +98 -0
- package/src/client/js/editor/link-wrap.ts +45 -0
- package/src/client/js/editor/markdown-shortcuts.ts +261 -0
- package/src/client/js/handlers/admin.ts +246 -0
- package/src/client/js/state/dialogs.svelte.ts +35 -0
- package/src/client/js/state/router.svelte.ts +156 -0
- package/src/client/js/state/schema.svelte.ts +140 -0
- package/src/client/js/state/state.svelte.ts +334 -0
- package/src/client/js/state/theme.svelte.ts +173 -0
- package/src/client/js/storage/adapter.ts +102 -0
- package/src/client/js/storage/client.ts +150 -0
- package/src/client/js/storage/db.ts +36 -0
- package/src/client/js/storage/fsa.ts +110 -0
- package/src/client/js/storage/github.ts +297 -0
- package/src/client/js/storage/storage.ts +83 -0
- package/src/client/js/storage/workers/frontmatter.ts +320 -0
- package/src/client/js/storage/workers/storage.ts +177 -0
- package/src/client/js/storage/workers/toml-parser.ts +106 -0
- package/src/client/js/storage/workers/yaml-parser.ts +132 -0
- package/src/client/js/utils/file-types.ts +192 -0
- package/src/client/js/utils/format.ts +16 -0
- package/src/client/js/utils/frontmatter.ts +38 -0
- package/src/client/js/utils/schema-utils.ts +295 -0
- package/src/client/js/utils/slug.ts +18 -0
- package/src/client/js/utils/sort.ts +84 -0
- package/src/client/js/utils/stable-stringify.ts +27 -0
- package/src/client/js/utils/url-utils.ts +38 -0
- package/src/types.ts +25 -0
- package/src/virtual.d.ts +22 -0
- package/svelte.config.js +4 -0
- package/tests/astro/build.test.ts +63 -0
- package/tests/astro/index.test.ts +689 -0
- package/tests/client/components/Admin.test.ts +446 -0
- package/tests/client/components/BackendPicker.test.ts +239 -0
- package/tests/client/components/DraftChip.test.ts +53 -0
- package/tests/client/components/MetadataForm.test.ts +164 -0
- package/tests/client/components/dialogs/DeleteDraftDialog.test.ts +91 -0
- package/tests/client/components/dialogs/FilenameDialog.test.ts +209 -0
- package/tests/client/components/dialogs/dialog-stubs.ts +19 -0
- package/tests/client/components/editor/EditorPane.test.ts +100 -0
- package/tests/client/components/editor/EditorTabs.test.ts +253 -0
- package/tests/client/components/editor/EditorToolbar.test.ts +252 -0
- package/tests/client/components/editor/fixtures.ts +31 -0
- package/tests/client/components/fields/ArrayField.test.ts +197 -0
- package/tests/client/components/fields/BooleanField.test.ts +206 -0
- package/tests/client/components/fields/DateField.test.ts +210 -0
- package/tests/client/components/fields/EnumField.test.ts +246 -0
- package/tests/client/components/fields/NumberField.test.ts +240 -0
- package/tests/client/components/fields/ObjectField.test.ts +157 -0
- package/tests/client/components/fields/SchemaField.test.ts +190 -0
- package/tests/client/components/fields/StringField.test.ts +223 -0
- package/tests/client/components/sidebar/AdminSidebar.test.ts +285 -0
- package/tests/client/components/sidebar/AdminSidebarSort.test.ts +135 -0
- package/tests/client/components/sidebar/sort-mock.ts +23 -0
- package/tests/client/js/drafts/fixtures.ts +22 -0
- package/tests/client/js/drafts/merge.test.ts +282 -0
- package/tests/client/js/drafts/ops.test.ts +658 -0
- package/tests/client/js/drafts/storage.test.ts +200 -0
- package/tests/client/js/drafts/workers/diff.test.ts +165 -0
- package/tests/client/js/editor/editor.test.ts +616 -0
- package/tests/client/js/editor/link-wrap.test.ts +225 -0
- package/tests/client/js/editor/markdown-shortcuts.test.ts +370 -0
- package/tests/client/js/handlers/admin.test.ts +467 -0
- package/tests/client/js/state/router.test.ts +619 -0
- package/tests/client/js/state/schema.test.ts +266 -0
- package/tests/client/js/state/state.test.ts +328 -0
- package/tests/client/js/storage/adapter.test.ts +115 -0
- package/tests/client/js/storage/client.test.ts +250 -0
- package/tests/client/js/storage/db.test.ts +59 -0
- package/tests/client/js/storage/fsa.test.ts +284 -0
- package/tests/client/js/storage/github.test.ts +349 -0
- package/tests/client/js/storage/mock-port.ts +95 -0
- package/tests/client/js/storage/storage.test.ts +77 -0
- package/tests/client/js/storage/workers/frontmatter.test.ts +479 -0
- package/tests/client/js/storage/workers/storage.test.ts +299 -0
- package/tests/client/js/storage/workers/toml-parser.test.ts +169 -0
- package/tests/client/js/storage/workers/yaml-parser.test.ts +168 -0
- package/tests/client/js/utils/file-types.test.ts +268 -0
- package/tests/client/js/utils/frontmatter.test.ts +87 -0
- package/tests/client/js/utils/schema-utils.test.ts +318 -0
- package/tests/client/js/utils/slug.test.ts +58 -0
- package/tests/client/js/utils/sort.test.ts +276 -0
- package/tests/client/js/utils/stable-stringify.test.ts +68 -0
- package/tests/client/js/utils/url-utils.test.ts +70 -0
- package/tests/e2e/backend-connection.test.ts +301 -0
- package/tests/e2e/draft-lifecycle.test.ts +388 -0
- package/tests/e2e/editing.test.ts +355 -0
- package/tests/e2e/github-adapter.test.ts +330 -0
- package/tests/e2e/helpers/mock-adapter.ts +166 -0
- package/tests/e2e/helpers/test-app.ts +155 -0
- package/tests/e2e/navigation.test.ts +358 -0
- package/tests/e2e/publishing.test.ts +345 -0
- package/tests/e2e/unsaved-changes.test.ts +317 -0
- package/tests/setup.ts +2 -0
- package/tests/stubs/codemirror.ts +197 -0
- package/tsconfig.json +19 -0
- package/vitest.config.ts +178 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
2
|
+
import { render, cleanup, fireEvent } from '@testing-library/svelte';
|
|
3
|
+
import EditorTabs from '../../../../src/client/components/editor/EditorTabs.svelte';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Tests for the EditorTabs component.
|
|
7
|
+
* Mocks schema-utils and editor.svelte to control tab data without
|
|
8
|
+
* triggering Svelte 5 rune initialization in the jsdom environment.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// vi.hoisted ensures these declarations are available when vi.mock factories run,
|
|
12
|
+
// since vi.mock calls are hoisted to the top of the file by Vitest.
|
|
13
|
+
const { mockActiveTab, mockSetActiveTab, mockExtractTabs, mockEditorFile } =
|
|
14
|
+
vi.hoisted(() => ({
|
|
15
|
+
mockActiveTab: vi.fn(() => 'metadata'),
|
|
16
|
+
mockSetActiveTab: vi.fn(),
|
|
17
|
+
mockExtractTabs: vi.fn(() => [] as string[]),
|
|
18
|
+
mockEditorFile: vi.fn(() => null),
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
vi.mock('../../../../src/client/js/utils/schema-utils', () => ({
|
|
22
|
+
extractTabs: mockExtractTabs,
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
vi.mock('../../../../src/client/js/editor/editor.svelte', () => ({
|
|
26
|
+
editor: {
|
|
27
|
+
get tab() {
|
|
28
|
+
return mockActiveTab();
|
|
29
|
+
},
|
|
30
|
+
get data() {
|
|
31
|
+
return {};
|
|
32
|
+
},
|
|
33
|
+
get originalFilename() {
|
|
34
|
+
return '';
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
setActiveTab: mockSetActiveTab,
|
|
38
|
+
getEditorFile: mockEditorFile,
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
// Prevent accumulated renders from bleeding between tests
|
|
42
|
+
afterEach(() => cleanup());
|
|
43
|
+
|
|
44
|
+
describe('EditorTabs', () => {
|
|
45
|
+
/*
|
|
46
|
+
//////////////////////////////
|
|
47
|
+
// Default tabs
|
|
48
|
+
//////////////////////////////
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
it('always renders the Metadata and Body tabs', () => {
|
|
52
|
+
const { container } = render(EditorTabs, {
|
|
53
|
+
props: { schema: null },
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const buttons = Array.from(container.querySelectorAll('.tabs__tab'));
|
|
57
|
+
const labels = buttons.map((b) => b.textContent?.trim());
|
|
58
|
+
|
|
59
|
+
expect(labels).toContain('Metadata');
|
|
60
|
+
expect(labels).toContain('Body');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('renders Metadata before Body in tab order', () => {
|
|
64
|
+
const { container } = render(EditorTabs, {
|
|
65
|
+
props: { schema: null },
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const buttons = Array.from(container.querySelectorAll('.tabs__tab'));
|
|
69
|
+
const labels = buttons.map((b) => b.textContent?.trim());
|
|
70
|
+
|
|
71
|
+
expect(labels.indexOf('Metadata')).toBeLessThan(labels.indexOf('Body'));
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
//////////////////////////////
|
|
76
|
+
// Schema-derived custom tabs
|
|
77
|
+
//////////////////////////////
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
it('renders custom tabs returned by extractTabs', () => {
|
|
81
|
+
mockExtractTabs.mockReturnValueOnce(['seo', 'social']);
|
|
82
|
+
|
|
83
|
+
const { container } = render(EditorTabs, {
|
|
84
|
+
props: {
|
|
85
|
+
schema: {
|
|
86
|
+
properties: {
|
|
87
|
+
seoTitle: { type: 'string', tab: ['seo'] },
|
|
88
|
+
ogImage: { type: 'string', tab: ['social'] },
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const buttons = Array.from(container.querySelectorAll('.tabs__tab'));
|
|
95
|
+
const labels = buttons.map((b) => b.textContent?.trim());
|
|
96
|
+
|
|
97
|
+
expect(labels).toContain('Seo');
|
|
98
|
+
expect(labels).toContain('Social');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('capitalizes custom tab labels', () => {
|
|
102
|
+
mockExtractTabs.mockReturnValueOnce(['advanced']);
|
|
103
|
+
|
|
104
|
+
const { container } = render(EditorTabs, {
|
|
105
|
+
props: { schema: { properties: {} } },
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const buttons = Array.from(container.querySelectorAll('.tabs__tab'));
|
|
109
|
+
const labels = buttons.map((b) => b.textContent?.trim());
|
|
110
|
+
expect(labels).toContain('Advanced');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('renders no custom tabs when schema is null', () => {
|
|
114
|
+
mockExtractTabs.mockReturnValueOnce([]);
|
|
115
|
+
|
|
116
|
+
const { container } = render(EditorTabs, {
|
|
117
|
+
props: { schema: null },
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const buttons = container.querySelectorAll('.tabs__tab');
|
|
121
|
+
// Only Metadata and Body should be present
|
|
122
|
+
expect(buttons.length).toBe(2);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
/*
|
|
126
|
+
//////////////////////////////
|
|
127
|
+
// Active tab state
|
|
128
|
+
//////////////////////////////
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
it('marks the active tab with tabs__tab--active class', () => {
|
|
132
|
+
mockActiveTab.mockReturnValue('body');
|
|
133
|
+
mockExtractTabs.mockReturnValueOnce([]);
|
|
134
|
+
|
|
135
|
+
const { container } = render(EditorTabs, {
|
|
136
|
+
props: { schema: null },
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const buttons = Array.from(container.querySelectorAll('.tabs__tab'));
|
|
140
|
+
const active = buttons.find((b) =>
|
|
141
|
+
b.classList.contains('tabs__tab--active'),
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
expect(active?.textContent?.trim()).toBe('Body');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('sets aria-selected="true" on the active tab', () => {
|
|
148
|
+
mockActiveTab.mockReturnValue('metadata');
|
|
149
|
+
mockExtractTabs.mockReturnValueOnce([]);
|
|
150
|
+
|
|
151
|
+
const { container } = render(EditorTabs, {
|
|
152
|
+
props: { schema: null },
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const buttons = Array.from(container.querySelectorAll('.tabs__tab'));
|
|
156
|
+
const activeBtn = buttons.find(
|
|
157
|
+
(b) => b.getAttribute('aria-selected') === 'true',
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
expect(activeBtn?.textContent?.trim()).toBe('Metadata');
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
/*
|
|
164
|
+
//////////////////////////////
|
|
165
|
+
// Click interaction
|
|
166
|
+
//////////////////////////////
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
it('calls setActiveTab with the clicked tab identifier', async () => {
|
|
170
|
+
mockSetActiveTab.mockClear();
|
|
171
|
+
mockActiveTab.mockReturnValue('metadata');
|
|
172
|
+
mockExtractTabs.mockReturnValueOnce([]);
|
|
173
|
+
|
|
174
|
+
const { container } = render(EditorTabs, {
|
|
175
|
+
props: { schema: null },
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const buttons = Array.from(
|
|
179
|
+
container.querySelectorAll('.tabs__tab'),
|
|
180
|
+
) as HTMLButtonElement[];
|
|
181
|
+
const bodyBtn = buttons.find((b) => b.textContent?.trim() === 'Body')!;
|
|
182
|
+
await fireEvent.click(bodyBtn);
|
|
183
|
+
|
|
184
|
+
expect(mockSetActiveTab).toHaveBeenCalledOnce();
|
|
185
|
+
expect(mockSetActiveTab).toHaveBeenCalledWith('body');
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('calls setActiveTab with "metadata" when the Metadata tab is clicked', async () => {
|
|
189
|
+
mockSetActiveTab.mockClear();
|
|
190
|
+
mockActiveTab.mockReturnValue('body');
|
|
191
|
+
mockExtractTabs.mockReturnValueOnce([]);
|
|
192
|
+
|
|
193
|
+
const { container } = render(EditorTabs, {
|
|
194
|
+
props: { schema: null },
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const buttons = Array.from(
|
|
198
|
+
container.querySelectorAll('.tabs__tab'),
|
|
199
|
+
) as HTMLButtonElement[];
|
|
200
|
+
const metaBtn = buttons.find((b) => b.textContent?.trim() === 'Metadata')!;
|
|
201
|
+
await fireEvent.click(metaBtn);
|
|
202
|
+
|
|
203
|
+
expect(mockSetActiveTab).toHaveBeenCalledWith('metadata');
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
/*
|
|
207
|
+
//////////////////////////////
|
|
208
|
+
// Body tab visibility by file type
|
|
209
|
+
//////////////////////////////
|
|
210
|
+
*/
|
|
211
|
+
|
|
212
|
+
it('shows the Body tab when the open file is a .md file', () => {
|
|
213
|
+
mockEditorFile.mockReturnValueOnce({ filename: 'post.md' });
|
|
214
|
+
mockExtractTabs.mockReturnValueOnce([]);
|
|
215
|
+
|
|
216
|
+
const { container } = render(EditorTabs, {
|
|
217
|
+
props: { schema: null },
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const buttons = Array.from(container.querySelectorAll('.tabs__tab'));
|
|
221
|
+
const labels = buttons.map((b) => b.textContent?.trim());
|
|
222
|
+
|
|
223
|
+
expect(labels).toContain('Body');
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('hides the Body tab when the open file is a .json file', () => {
|
|
227
|
+
mockEditorFile.mockReturnValueOnce({ filename: 'data.json' });
|
|
228
|
+
mockExtractTabs.mockReturnValueOnce([]);
|
|
229
|
+
|
|
230
|
+
const { container } = render(EditorTabs, {
|
|
231
|
+
props: { schema: null },
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const buttons = Array.from(container.querySelectorAll('.tabs__tab'));
|
|
235
|
+
const labels = buttons.map((b) => b.textContent?.trim());
|
|
236
|
+
|
|
237
|
+
expect(labels).not.toContain('Body');
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('shows the Body tab when no file is open (fallback default)', () => {
|
|
241
|
+
mockEditorFile.mockReturnValueOnce(null);
|
|
242
|
+
mockExtractTabs.mockReturnValueOnce([]);
|
|
243
|
+
|
|
244
|
+
const { container } = render(EditorTabs, {
|
|
245
|
+
props: { schema: null },
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
const buttons = Array.from(container.querySelectorAll('.tabs__tab'));
|
|
249
|
+
const labels = buttons.map((b) => b.textContent?.trim());
|
|
250
|
+
|
|
251
|
+
expect(labels).toContain('Body');
|
|
252
|
+
});
|
|
253
|
+
});
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
2
|
+
import { render, cleanup, fireEvent } from '@testing-library/svelte';
|
|
3
|
+
import EditorToolbar from '../../../../src/client/components/editor/EditorToolbar.svelte';
|
|
4
|
+
import { makeEditorFile } from './fixtures';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Tests for the EditorToolbar component.
|
|
8
|
+
* The component imports state modules directly rather than receiving props,
|
|
9
|
+
* so we mock each module to control rendering and verify interactions.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// vi.hoisted ensures these declarations are available when vi.mock factories run,
|
|
13
|
+
// since vi.mock calls are hoisted to the top of the file by Vitest.
|
|
14
|
+
const {
|
|
15
|
+
mockGetEditorFile,
|
|
16
|
+
mockHandleSave,
|
|
17
|
+
mockHandlePublish,
|
|
18
|
+
mockOpenDialog,
|
|
19
|
+
mockComputePublishDisabled,
|
|
20
|
+
} = vi.hoisted(() => ({
|
|
21
|
+
mockGetEditorFile: vi.fn(),
|
|
22
|
+
mockHandleSave: vi.fn(),
|
|
23
|
+
mockHandlePublish: vi.fn(() => Promise.resolve({ status: 'ok' as const })),
|
|
24
|
+
mockOpenDialog: vi.fn(),
|
|
25
|
+
mockComputePublishDisabled: vi.fn(() => false),
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
vi.mock('../../../../src/client/js/editor/editor.svelte', () => ({
|
|
29
|
+
getEditorFile: mockGetEditorFile,
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
vi.mock('../../../../src/client/js/state/router.svelte', () => ({
|
|
33
|
+
nav: {
|
|
34
|
+
get route() {
|
|
35
|
+
return { view: 'file', collection: 'posts', slug: 'hello' };
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
vi.mock('../../../../src/client/js/state/schema.svelte', () => ({
|
|
41
|
+
schema: {
|
|
42
|
+
get active() {
|
|
43
|
+
return null;
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
}));
|
|
47
|
+
|
|
48
|
+
vi.mock('../../../../src/client/js/handlers/admin', () => ({
|
|
49
|
+
handleSave: mockHandleSave,
|
|
50
|
+
handlePublish: mockHandlePublish,
|
|
51
|
+
computePublishDisabled: mockComputePublishDisabled,
|
|
52
|
+
}));
|
|
53
|
+
|
|
54
|
+
vi.mock('../../../../src/client/js/state/dialogs.svelte', () => ({
|
|
55
|
+
dialog: {
|
|
56
|
+
open: mockOpenDialog,
|
|
57
|
+
},
|
|
58
|
+
}));
|
|
59
|
+
|
|
60
|
+
// Prevent accumulated renders from bleeding between tests
|
|
61
|
+
afterEach(() => cleanup());
|
|
62
|
+
|
|
63
|
+
describe('EditorToolbar', () => {
|
|
64
|
+
/*
|
|
65
|
+
//////////////////////////////
|
|
66
|
+
// Renders when file is open
|
|
67
|
+
//////////////////////////////
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
it('renders the toolbar header when a file is open', () => {
|
|
71
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile());
|
|
72
|
+
|
|
73
|
+
const { container } = render(EditorToolbar);
|
|
74
|
+
|
|
75
|
+
expect(container.querySelector('.toolbar')).not.toBeNull();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('renders nothing when no file is open', () => {
|
|
79
|
+
mockGetEditorFile.mockReturnValue(null);
|
|
80
|
+
|
|
81
|
+
const { container } = render(EditorToolbar);
|
|
82
|
+
|
|
83
|
+
expect(container.querySelector('.toolbar')).toBeNull();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
/*
|
|
87
|
+
//////////////////////////////
|
|
88
|
+
// Title display
|
|
89
|
+
//////////////////////////////
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
it('shows the title from formData when present', () => {
|
|
93
|
+
mockGetEditorFile.mockReturnValue(
|
|
94
|
+
makeEditorFile({ formData: { title: 'My Great Post' } }),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const { container } = render(EditorToolbar);
|
|
98
|
+
|
|
99
|
+
expect(container.querySelector('.toolbar__title')?.textContent).toContain(
|
|
100
|
+
'My Great Post',
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('falls back to the filename when formData has no title', () => {
|
|
105
|
+
mockGetEditorFile.mockReturnValue(
|
|
106
|
+
makeEditorFile({ filename: 'my-post.md' }),
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const { container } = render(EditorToolbar);
|
|
110
|
+
|
|
111
|
+
expect(container.querySelector('.toolbar__title')?.textContent).toContain(
|
|
112
|
+
'my-post.md',
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('shows "Untitled Draft" when there is no title and no filename', () => {
|
|
117
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile({ filename: '' }));
|
|
118
|
+
|
|
119
|
+
const { container } = render(EditorToolbar);
|
|
120
|
+
|
|
121
|
+
expect(container.querySelector('.toolbar__title')?.textContent).toContain(
|
|
122
|
+
'Untitled Draft',
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
/*
|
|
127
|
+
//////////////////////////////
|
|
128
|
+
// Save button
|
|
129
|
+
//////////////////////////////
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
it('renders the Save button', () => {
|
|
133
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile());
|
|
134
|
+
|
|
135
|
+
const { container } = render(EditorToolbar);
|
|
136
|
+
|
|
137
|
+
const saveBtn = container.querySelector('.btn--save-outline');
|
|
138
|
+
expect(saveBtn).not.toBeNull();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('disables Save when file is not dirty', () => {
|
|
142
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile({ dirty: false }));
|
|
143
|
+
|
|
144
|
+
const { container } = render(EditorToolbar);
|
|
145
|
+
|
|
146
|
+
const saveBtn = container.querySelector(
|
|
147
|
+
'.btn--save-outline',
|
|
148
|
+
) as HTMLButtonElement;
|
|
149
|
+
expect(saveBtn?.disabled).toBe(true);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('calls handleSave when the Save button is clicked', async () => {
|
|
153
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile({ dirty: true }));
|
|
154
|
+
|
|
155
|
+
const { container } = render(EditorToolbar);
|
|
156
|
+
|
|
157
|
+
await fireEvent.click(container.querySelector('.btn--save-outline')!);
|
|
158
|
+
expect(mockHandleSave).toHaveBeenCalledOnce();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
/*
|
|
162
|
+
//////////////////////////////
|
|
163
|
+
// Publish button
|
|
164
|
+
//////////////////////////////
|
|
165
|
+
*/
|
|
166
|
+
|
|
167
|
+
it('renders the Publish button', () => {
|
|
168
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile());
|
|
169
|
+
|
|
170
|
+
const { container } = render(EditorToolbar);
|
|
171
|
+
|
|
172
|
+
expect(container.querySelector('.btn--primary')).not.toBeNull();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('disables the Publish button when computePublishDisabled returns true', () => {
|
|
176
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile());
|
|
177
|
+
mockComputePublishDisabled.mockReturnValue(true);
|
|
178
|
+
|
|
179
|
+
const { container } = render(EditorToolbar);
|
|
180
|
+
|
|
181
|
+
const publishBtn = container.querySelector(
|
|
182
|
+
'.btn--primary',
|
|
183
|
+
) as HTMLButtonElement;
|
|
184
|
+
expect(publishBtn?.disabled).toBe(true);
|
|
185
|
+
|
|
186
|
+
mockComputePublishDisabled.mockReturnValue(false);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('enables the Publish button when not disabled and not saving', () => {
|
|
190
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile({ saving: false }));
|
|
191
|
+
mockComputePublishDisabled.mockReturnValue(false);
|
|
192
|
+
|
|
193
|
+
const { container } = render(EditorToolbar);
|
|
194
|
+
|
|
195
|
+
const publishBtn = container.querySelector(
|
|
196
|
+
'.btn--primary',
|
|
197
|
+
) as HTMLButtonElement;
|
|
198
|
+
expect(publishBtn?.disabled).toBe(false);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('calls handlePublish when the Publish button is clicked', async () => {
|
|
202
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile());
|
|
203
|
+
|
|
204
|
+
const { container } = render(EditorToolbar);
|
|
205
|
+
|
|
206
|
+
await fireEvent.click(container.querySelector('.btn--primary')!);
|
|
207
|
+
expect(mockHandlePublish).toHaveBeenCalledOnce();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('shows filename dialog when publish needs a filename', async () => {
|
|
211
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile());
|
|
212
|
+
mockHandlePublish.mockResolvedValueOnce({ status: 'needs-filename' });
|
|
213
|
+
|
|
214
|
+
const { container } = render(EditorToolbar);
|
|
215
|
+
|
|
216
|
+
await fireEvent.click(container.querySelector('.btn--primary')!);
|
|
217
|
+
expect(mockOpenDialog).toHaveBeenCalledWith('filename');
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
/*
|
|
221
|
+
//////////////////////////////
|
|
222
|
+
// Delete Draft button
|
|
223
|
+
//////////////////////////////
|
|
224
|
+
*/
|
|
225
|
+
|
|
226
|
+
it('renders the Delete Draft button when draftId is set', () => {
|
|
227
|
+
mockGetEditorFile.mockReturnValue(
|
|
228
|
+
makeEditorFile({ draftId: 'draft-abc-123' }),
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
const { container } = render(EditorToolbar);
|
|
232
|
+
|
|
233
|
+
expect(container.querySelector('.btn--danger-outline')).not.toBeNull();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('does not render the Delete Draft button when draftId is null', () => {
|
|
237
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile({ draftId: null }));
|
|
238
|
+
|
|
239
|
+
const { container } = render(EditorToolbar);
|
|
240
|
+
|
|
241
|
+
expect(container.querySelector('.btn--danger-outline')).toBeNull();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('calls showDeleteDialog when the Delete Draft button is clicked', async () => {
|
|
245
|
+
mockGetEditorFile.mockReturnValue(makeEditorFile({ draftId: 'draft-abc' }));
|
|
246
|
+
|
|
247
|
+
const { container } = render(EditorToolbar);
|
|
248
|
+
|
|
249
|
+
await fireEvent.click(container.querySelector('.btn--danger-outline')!);
|
|
250
|
+
expect(mockOpenDialog).toHaveBeenCalledWith('delete');
|
|
251
|
+
});
|
|
252
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/* Shared test fixtures for editor component tests. */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Builds a minimal EditorFile fixture with sensible defaults.
|
|
5
|
+
* @param {Partial<Record<string, unknown>>} overrides - Fields to override on the default file
|
|
6
|
+
* @return {Record<string, unknown>} A minimal EditorFile-like object
|
|
7
|
+
*/
|
|
8
|
+
export function makeEditorFile(
|
|
9
|
+
overrides: Partial<{
|
|
10
|
+
filename: string;
|
|
11
|
+
dirty: boolean;
|
|
12
|
+
saving: boolean;
|
|
13
|
+
draftId: string | null;
|
|
14
|
+
formData: Record<string, unknown>;
|
|
15
|
+
body: string;
|
|
16
|
+
bodyLoaded: boolean;
|
|
17
|
+
isNewDraft: boolean;
|
|
18
|
+
}> = {},
|
|
19
|
+
) {
|
|
20
|
+
return {
|
|
21
|
+
filename: 'my-post.md',
|
|
22
|
+
dirty: false,
|
|
23
|
+
saving: false,
|
|
24
|
+
draftId: null,
|
|
25
|
+
formData: {},
|
|
26
|
+
body: '',
|
|
27
|
+
bodyLoaded: true,
|
|
28
|
+
isNewDraft: false,
|
|
29
|
+
...overrides,
|
|
30
|
+
};
|
|
31
|
+
}
|