|
| 1 | +import { relative, resolve } from 'pathe' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import { Context } from '../src/core/context' |
| 4 | + |
| 5 | +const root = resolve(__dirname, '../examples/vite-vue3') |
| 6 | + |
| 7 | +function cleanup(data: any) { |
| 8 | + return Object.values(data).map((e: any) => { |
| 9 | + delete e.absolute |
| 10 | + e.from = relative(root, e.from).replace(/\\/g, '/') |
| 11 | + return e |
| 12 | + }) |
| 13 | +} |
| 14 | + |
| 15 | +describe('sort', () => { |
| 16 | + it('sort ascending works', async () => { |
| 17 | + const ctx = new Context({ |
| 18 | + dirs: ['src/components/ui'], |
| 19 | + sort(_, files) { |
| 20 | + return files.toSorted((a, b) => a.localeCompare(b)) |
| 21 | + }, |
| 22 | + }) |
| 23 | + ctx.setRoot(root) |
| 24 | + ctx.searchGlob() |
| 25 | + |
| 26 | + expect(cleanup(ctx.componentNameMap)).toMatchInlineSnapshot(` |
| 27 | + [ |
| 28 | + { |
| 29 | + "as": "Button", |
| 30 | + "from": "src/components/ui/button.vue", |
| 31 | + }, |
| 32 | + { |
| 33 | + "as": "Checkbox", |
| 34 | + "from": "src/components/ui/nested/checkbox.vue", |
| 35 | + }, |
| 36 | + ] |
| 37 | + `) |
| 38 | + |
| 39 | + // simulate the watcher adding a components |
| 40 | + ctx.addComponents(resolve(root, 'src/components/book/index.vue').replace(/\\/g, '/')) |
| 41 | + ctx.searchGlob() |
| 42 | + |
| 43 | + expect(cleanup(ctx.componentNameMap)).toMatchInlineSnapshot(` |
| 44 | + [ |
| 45 | + { |
| 46 | + "as": "Book", |
| 47 | + "from": "src/components/book/index.vue", |
| 48 | + }, |
| 49 | + { |
| 50 | + "as": "Button", |
| 51 | + "from": "src/components/ui/button.vue", |
| 52 | + }, |
| 53 | + { |
| 54 | + "as": "Checkbox", |
| 55 | + "from": "src/components/ui/nested/checkbox.vue", |
| 56 | + }, |
| 57 | + ] |
| 58 | + `) |
| 59 | + }) |
| 60 | + |
| 61 | + it('sort descending works', async () => { |
| 62 | + const ctx = new Context({ |
| 63 | + dirs: ['src/components/book'], |
| 64 | + sort(_, files) { |
| 65 | + return files.toSorted((a, b) => b.localeCompare(a)) |
| 66 | + }, |
| 67 | + }) |
| 68 | + ctx.setRoot(root) |
| 69 | + ctx.searchGlob() |
| 70 | + |
| 71 | + expect(cleanup(ctx.componentNameMap)).toMatchInlineSnapshot(` |
| 72 | + [ |
| 73 | + { |
| 74 | + "as": "Book", |
| 75 | + "from": "src/components/book/index.vue", |
| 76 | + }, |
| 77 | + ] |
| 78 | + `) |
| 79 | + |
| 80 | + // simulate the watcher adding a components |
| 81 | + ctx.addComponents(resolve(root, 'src/components/ui/button.vue').replace(/\\/g, '/')) |
| 82 | + ctx.addComponents(resolve(root, 'src/components/ui/nested/checkbox.vue').replace(/\\/g, '/')) |
| 83 | + ctx.searchGlob() |
| 84 | + |
| 85 | + expect(cleanup(ctx.componentNameMap)).toMatchInlineSnapshot(` |
| 86 | + [ |
| 87 | + { |
| 88 | + "as": "Checkbox", |
| 89 | + "from": "src/components/ui/nested/checkbox.vue", |
| 90 | + }, |
| 91 | + { |
| 92 | + "as": "Button", |
| 93 | + "from": "src/components/ui/button.vue", |
| 94 | + }, |
| 95 | + { |
| 96 | + "as": "Book", |
| 97 | + "from": "src/components/book/index.vue", |
| 98 | + }, |
| 99 | + ] |
| 100 | + `) |
| 101 | + }) |
| 102 | +}) |
0 commit comments