Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { finalizeAddOns, getAllAddOns } from '../src/add-ons.js'
4
5import type { AddOn, Framework } from '../src/types.js'
6
7describe('getAllAddOns', () => {
8 it('filter add-ons', () => {
9 const addOns = getAllAddOns(
10 {
11 id: 'react-cra',
12 getAddOns: () => [
13 {
14 id: 'add-on-1',
15 description: 'Add-on 1',
16 modes: ['file-router'],
17 } as AddOn,
18 {
19 id: 'add-on-2',
20 description: 'Add-on 2',
21 modes: ['code-router'],
22 } as AddOn,
23 ],
24 } as Framework,
25 'file-router',
26 )
27
28 expect(addOns.length).toEqual(1)
29 expect(addOns[0].id).toEqual('add-on-1')
30 })
31
32 it('should sort add-ons by priority (higher priority first)', () => {
33 const addOns = getAllAddOns(
34 {
35 id: 'react-cra',
36 getAddOns: () => [
37 {
38 id: 'low-priority',
39 description: 'Low Priority',
40 modes: ['file-router'],
41 priority: 10,
42 } as AddOn,
43 {
44 id: 'high-priority',
45 description: 'High Priority',
46 modes: ['file-router'],
47 priority: 100,
48 } as AddOn,
49 {
50 id: 'no-priority',
51 description: 'No Priority (defaults to 0)',
52 modes: ['file-router'],
53 } as AddOn,
54 {
55 id: 'medium-priority',
56 description: 'Medium Priority',
57 modes: ['file-router'],
58 priority: 50,
59 } as AddOn,
60 ],
61 } as Framework,
62 'file-router',
63 )
64
65 expect(addOns.length).toEqual(4)
66 expect(addOns[0].id).toEqual('high-priority')
67 expect(addOns[1].id).toEqual('medium-priority')
68 expect(addOns[2].id).toEqual('low-priority')
69 expect(addOns[3].id).toEqual('no-priority')
70 })
71
72 it('should filter by mode and then sort by priority', () => {
73 const addOns = getAllAddOns(
74 {
75 id: 'react-cra',
76 getAddOns: () => [
77 {
78 id: 'file-router-low',
79 description: 'File Router Low',
80 modes: ['file-router'],
81 priority: 20,
82 } as AddOn,
83 {
84 id: 'code-router-high',
85 description: 'Code Router High (should be filtered out)',
86 modes: ['code-router'],
87 priority: 200,
88 } as AddOn,
89 {
90 id: 'file-router-high',
91 description: 'File Router High',
92 modes: ['file-router'],
93 priority: 100,
94 } as AddOn,
95 ],
96 } as Framework,
97 'file-router',
98 )
99
100 expect(addOns.length).toEqual(2)
101 expect(addOns[0].id).toEqual('file-router-high')
102 expect(addOns[1].id).toEqual('file-router-low')
103 })
104})
105
106describe('finalizeAddOns', () => {
107 it('should finalize add-ons', async () => {
108 const addOns = await finalizeAddOns(
109 {
110 id: 'react-cra',
111 getAddOns: () => [
112 {
113 id: 'add-on-1',
114 description: 'Add-on 1',
115 modes: ['file-router'],
116 dependsOn: ['add-on-2'],
117 } as AddOn,
118 {
119 id: 'add-on-2',
120 description: 'Add-on 2',
121 modes: ['file-router'],
122 } as AddOn,
123 {
124 id: 'add-on-3',
125 description: 'Add-on 3',
126 modes: ['file-router'],
127 } as AddOn,
128 ],
129 } as Framework,
130 'file-router',
131 ['add-on-1'],
132 )
133
134 expect(addOns.length).toEqual(2)
135 const addOnIds = addOns.map((a) => a.id)
136 expect(addOnIds.includes('add-on-1')).toEqual(true)
137 expect(addOnIds.includes('add-on-2')).toEqual(true)
138 expect(addOnIds.includes('add-on-3')).toEqual(false)
139 })
140})