|
| 1 | +import type { IndexEntry } from 'storybook/internal/types'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +vi.mock('./StorybookThemeProvider', () => ({ |
| 5 | + StorybookThemeProvider: vi.fn(), |
| 6 | +})); |
| 7 | +vi.mock('./docs/DocsContainerWrapper', () => ({ |
| 8 | + DocsContainerWrapper: vi.fn(), |
| 9 | +})); |
| 10 | +vi.mock('./docs/Story', () => ({ |
| 11 | + DocsStory: vi.fn(), |
| 12 | +})); |
| 13 | +vi.mock('./themes', () => ({ |
| 14 | + getThemes: () => ({}), |
| 15 | +})); |
| 16 | + |
| 17 | +const { default: preview } = await import('./preview'); |
| 18 | + |
| 19 | +const storySort = preview.parameters.options.storySort; |
| 20 | + |
| 21 | +interface EntryOptions { |
| 22 | + title: string; |
| 23 | + type?: 'story' | 'docs'; |
| 24 | + name?: string; |
| 25 | + tags?: string[]; |
| 26 | +} |
| 27 | + |
| 28 | +function entry(options: EntryOptions | string): IndexEntry { |
| 29 | + const opts: EntryOptions = |
| 30 | + typeof options === 'string' ? { title: options } : options; |
| 31 | + const { title, type = 'docs', name, tags = [] } = opts; |
| 32 | + const parts = title.split('/'); |
| 33 | + |
| 34 | + if (type === 'docs') { |
| 35 | + return { |
| 36 | + id: title.replace(/\s+/g, '-').toLowerCase(), |
| 37 | + importPath: '', |
| 38 | + storiesImports: [], |
| 39 | + title, |
| 40 | + type, |
| 41 | + name: name ?? parts[parts.length - 1], |
| 42 | + tags, |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + return { |
| 47 | + id: title.replace(/\s+/g, '-').toLowerCase(), |
| 48 | + importPath: '', |
| 49 | + subtype: 'story', |
| 50 | + title, |
| 51 | + type, |
| 52 | + name: name ?? parts[parts.length - 1], |
| 53 | + tags, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +describe('storySort', () => { |
| 58 | + describe('Components category ordering', () => { |
| 59 | + it('should sort Layout before Buttons', () => { |
| 60 | + const layout = entry('Components/Layout/Box'); |
| 61 | + const buttons = entry('Components/Buttons/Button'); |
| 62 | + |
| 63 | + expect(storySort(layout, buttons)).toBeLessThan(0); |
| 64 | + expect(storySort(buttons, layout)).toBeGreaterThan(0); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should sort Layout before Forms', () => { |
| 68 | + const layout = entry('Components/Layout/Box'); |
| 69 | + const forms = entry('Components/Forms/Input'); |
| 70 | + |
| 71 | + expect(storySort(layout, forms)).toBeLessThan(0); |
| 72 | + expect(storySort(forms, layout)).toBeGreaterThan(0); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should sort Buttons before Forms', () => { |
| 76 | + const buttons = entry('Components/Buttons/Button'); |
| 77 | + const forms = entry('Components/Forms/Input'); |
| 78 | + |
| 79 | + expect(storySort(buttons, forms)).toBeLessThan(0); |
| 80 | + expect(storySort(forms, buttons)).toBeGreaterThan(0); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('Guides category ordering', () => { |
| 85 | + it('should sort Introduction before Styling', () => { |
| 86 | + const intro = entry('Guides/Introduction'); |
| 87 | + const styling = entry('Guides/Styling'); |
| 88 | + |
| 89 | + expect(storySort(intro, styling)).toBeLessThan(0); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should sort Styling before Theming', () => { |
| 93 | + const styling = entry('Guides/Styling'); |
| 94 | + const theming = entry('Guides/Theming'); |
| 95 | + |
| 96 | + expect(storySort(styling, theming)).toBeLessThan(0); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('top-level category ordering', () => { |
| 101 | + it('should sort Guides before Components', () => { |
| 102 | + const guides = entry('Guides/Introduction'); |
| 103 | + const components = entry('Components/Layout/Box'); |
| 104 | + |
| 105 | + expect(storySort(guides, components)).toBeLessThan(0); |
| 106 | + expect(storySort(components, guides)).toBeGreaterThan(0); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('Components/Buttons ordering', () => { |
| 111 | + it('should sort Button before Close Button', () => { |
| 112 | + const button = entry('Components/Buttons/Button'); |
| 113 | + const closeButton = entry('Components/Buttons/Close Button'); |
| 114 | + |
| 115 | + expect(storySort(button, closeButton)).toBeLessThan(0); |
| 116 | + expect(storySort(closeButton, button)).toBeGreaterThan(0); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('Docs entries', () => { |
| 121 | + it('should sort Docs before other story names within the same path', () => { |
| 122 | + const docs = entry({ |
| 123 | + title: 'Components/Layout/Box', |
| 124 | + type: 'docs', |
| 125 | + name: 'Docs', |
| 126 | + }); |
| 127 | + const story = entry({ |
| 128 | + title: 'Components/Layout/Box', |
| 129 | + type: 'story', |
| 130 | + name: 'Example', |
| 131 | + }); |
| 132 | + |
| 133 | + expect(storySort(docs, story)).toBeLessThan(0); |
| 134 | + expect(storySort(story, docs)).toBeGreaterThan(0); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('attached-mdx vs unattached-mdx', () => { |
| 139 | + it('should sort Layout before Buttons even with different mdx attachment status', () => { |
| 140 | + const layout = entry({ |
| 141 | + title: 'Components/Layout/Box', |
| 142 | + name: 'Docs', |
| 143 | + tags: ['dev', 'test', 'unattached-mdx'], |
| 144 | + }); |
| 145 | + const buttons = entry({ |
| 146 | + title: 'Components/Buttons/Button', |
| 147 | + name: 'Docs', |
| 148 | + tags: ['dev', 'test', 'attached-mdx'], |
| 149 | + }); |
| 150 | + |
| 151 | + expect(storySort(layout, buttons)).toBeLessThan(0); |
| 152 | + expect(storySort(buttons, layout)).toBeGreaterThan(0); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should sort attached-mdx before unattached-mdx within the same subcategory', () => { |
| 156 | + const attached = entry({ |
| 157 | + title: 'Components/Layout/Box', |
| 158 | + name: 'Docs', |
| 159 | + tags: ['dev', 'test', 'attached-mdx'], |
| 160 | + }); |
| 161 | + const unattached = entry({ |
| 162 | + title: 'Components/Layout/Center', |
| 163 | + name: 'Docs', |
| 164 | + tags: ['dev', 'test', 'unattached-mdx'], |
| 165 | + }); |
| 166 | + |
| 167 | + // attached-mdx entries have name='Docs', unattached use path name |
| 168 | + // So attached should come first due to 'Docs' sorting |
| 169 | + expect(storySort(attached, unattached)).toBeLessThan(0); |
| 170 | + expect(storySort(unattached, attached)).toBeGreaterThan(0); |
| 171 | + }); |
| 172 | + }); |
| 173 | +}); |
0 commit comments