1 | const path = require('path');
|
2 | const { writeRequires, getMain, getPreviewExists } = require('./loader');
|
3 |
|
4 | let pathMock;
|
5 | let fileContentMock;
|
6 |
|
7 | jest.mock('fs', () => ({
|
8 | ...jest.requireActual('fs'),
|
9 | writeFileSync: (filePath, fileContent, opts) => {
|
10 | pathMock = filePath;
|
11 | fileContentMock = fileContent;
|
12 | },
|
13 | }));
|
14 |
|
15 | jest.mock('prettier', () => ({
|
16 | format(s, opts) {
|
17 | return s;
|
18 | },
|
19 | }));
|
20 |
|
21 | describe('loader', () => {
|
22 | describe('getMain', () => {
|
23 | it('should return the main js default export as an object', () => {
|
24 | const main = getMain({ configPath: path.resolve(__dirname, 'mocks/all-config-files') });
|
25 | expect(main).toEqual({
|
26 | stories: ['./FakeStory.stories.tsx'],
|
27 | addons: [
|
28 | '@storybook/addon-ondevice-notes',
|
29 | '@storybook/addon-ondevice-controls',
|
30 | '@storybook/addon-ondevice-backgrounds',
|
31 | '@storybook/addon-ondevice-actions',
|
32 | ],
|
33 | });
|
34 | });
|
35 |
|
36 | it('should also work with relative paths', () => {
|
37 |
|
38 | const main = getMain({ configPath: './scripts/mocks/all-config-files' });
|
39 | expect(main).toEqual({
|
40 | stories: ['./FakeStory.stories.tsx'],
|
41 | addons: [
|
42 | '@storybook/addon-ondevice-notes',
|
43 | '@storybook/addon-ondevice-controls',
|
44 | '@storybook/addon-ondevice-backgrounds',
|
45 | '@storybook/addon-ondevice-actions',
|
46 | ],
|
47 | });
|
48 | });
|
49 | it('should work for any supported file extension', () => {
|
50 | const main = getMain({ configPath: './scripts/mocks/file-extensions' });
|
51 | expect(main).toEqual({
|
52 | default: {
|
53 | stories: ['./FakeStory.stories.tsx'],
|
54 | addons: [
|
55 | '@storybook/addon-ondevice-notes',
|
56 | '@storybook/addon-ondevice-controls',
|
57 | '@storybook/addon-ondevice-backgrounds',
|
58 | '@storybook/addon-ondevice-actions',
|
59 | ],
|
60 | },
|
61 | });
|
62 | });
|
63 | });
|
64 |
|
65 | describe('getPreviewExists', () => {
|
66 | const supportedExtensions = ['js', 'jsx', 'ts', 'tsx'];
|
67 | describe('when using a relative path', () => {
|
68 | it('should return true if the preview exists', () => {
|
69 | supportedExtensions.forEach((ext) => {
|
70 | expect(getPreviewExists({ configPath: `scripts/mocks/preview-files/${ext}` })).toBe(true);
|
71 | });
|
72 | });
|
73 |
|
74 | it('should return false if the preview does not exist', () => {
|
75 | expect(getPreviewExists({ configPath: './scripts/mocks/no-preview' })).toBe(false);
|
76 | });
|
77 |
|
78 | it('should return false if the preview does not match any of supportedExtensions values', () => {
|
79 | expect(getPreviewExists({ configPath: './scripts/mocks/wrong-extension-preview' })).toBe(
|
80 | false
|
81 | );
|
82 | });
|
83 | });
|
84 |
|
85 | describe('when using an absolute path', () => {
|
86 | it('should return true if the preview exists', () => {
|
87 | supportedExtensions.forEach((ext) => {
|
88 | expect(
|
89 | getPreviewExists({
|
90 | configPath: path.resolve(__dirname, `mocks/preview-files/${ext}`),
|
91 | })
|
92 | ).toBe(true);
|
93 | });
|
94 | });
|
95 |
|
96 | it('should return false if the preview does not exist', () => {
|
97 | expect(getPreviewExists({ configPath: path.resolve(__dirname, 'mocks/no-preview') })).toBe(
|
98 | false
|
99 | );
|
100 | });
|
101 |
|
102 | it('should return false if the preview does not match any of supportedExtensions values', () => {
|
103 | expect(
|
104 | getPreviewExists({ configPath: path.resolve(__dirname, 'mocks/wrong-extension-preview') })
|
105 | ).toBe(false);
|
106 | });
|
107 | });
|
108 | });
|
109 |
|
110 | describe('writeRequires', () => {
|
111 | describe('when there is a story glob', () => {
|
112 | it('writes the story imports', () => {
|
113 | writeRequires({ configPath: 'scripts/mocks/all-config-files' });
|
114 | expect(pathMock).toEqual(
|
115 | path.resolve(__dirname, 'mocks/all-config-files/storybook.requires.js')
|
116 | );
|
117 | expect(fileContentMock).toMatchSnapshot();
|
118 | });
|
119 | });
|
120 |
|
121 | describe('when there are different file extensions', () => {
|
122 | it('writes the story imports', () => {
|
123 | writeRequires({ configPath: 'scripts/mocks/file-extensions' });
|
124 | expect(pathMock).toEqual(
|
125 | path.resolve(__dirname, 'mocks/file-extensions/storybook.requires.js')
|
126 | );
|
127 | expect(fileContentMock).toMatchSnapshot();
|
128 | });
|
129 | });
|
130 |
|
131 | describe('when there is a story glob and exclude paths globs', () => {
|
132 | it('writes the story imports', () => {
|
133 | writeRequires({ configPath: 'scripts/mocks/exclude-config-files' });
|
134 | expect(pathMock).toEqual(
|
135 | path.resolve(__dirname, 'mocks/exclude-config-files/storybook.requires.js')
|
136 | );
|
137 |
|
138 | expect(fileContentMock).toContain('include-components/FakeStory.stories.tsx');
|
139 | expect(fileContentMock).not.toContain('exclude-components/FakeStory.stories.tsx');
|
140 |
|
141 | expect(fileContentMock).toMatchSnapshot();
|
142 | });
|
143 | });
|
144 |
|
145 | describe('when there is no story glob or addons', () => {
|
146 | it('writes no story imports or addons', () => {
|
147 | writeRequires({ configPath: 'scripts/mocks/blank-config' });
|
148 | expect(pathMock).toEqual(
|
149 | path.resolve(__dirname, 'mocks/blank-config/storybook.requires.js')
|
150 | );
|
151 | expect(fileContentMock).toMatchSnapshot();
|
152 | });
|
153 | });
|
154 |
|
155 | describe('when there is no preview', () => {
|
156 | it('does not add preview related stuff', () => {
|
157 | writeRequires({ configPath: 'scripts/mocks/no-preview' });
|
158 | expect(pathMock).toEqual(path.resolve(__dirname, 'mocks/no-preview/storybook.requires.js'));
|
159 | expect(fileContentMock).toMatchSnapshot();
|
160 | });
|
161 | });
|
162 |
|
163 | describe('when the absolute option is true', () => {
|
164 | it('should write absolute paths to the requires file', () => {
|
165 | writeRequires({ configPath: 'scripts/mocks/all-config-files', absolute: true });
|
166 | expect(pathMock).toEqual(
|
167 | path.resolve(__dirname, 'mocks/all-config-files/storybook.requires.js')
|
168 | );
|
169 | expect(fileContentMock).toContain(
|
170 | path.resolve(__dirname, 'mocks/all-config-files/FakeStory.stories.tsx')
|
171 | );
|
172 | });
|
173 | });
|
174 |
|
175 | describe('when there is a configuration object', () => {
|
176 | it('writes the story imports', () => {
|
177 | writeRequires({ configPath: 'scripts/mocks/configuration-objects' });
|
178 | expect(pathMock).toEqual(
|
179 | path.resolve(__dirname, 'mocks/configuration-objects/storybook.requires.js')
|
180 | );
|
181 | expect(fileContentMock).toMatchSnapshot();
|
182 | });
|
183 | });
|
184 | });
|
185 | });
|