1 | const path = require('path');
|
2 | const { generate } = require('./generate');
|
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('writeRequires', () => {
|
23 | describe('when there is a story glob', () => {
|
24 | it('writes the story imports', () => {
|
25 | generate({ configPath: 'scripts/mocks/all-config-files' });
|
26 | expect(pathMock).toEqual(
|
27 | path.resolve(__dirname, 'mocks/all-config-files/storybook.requires.ts')
|
28 | );
|
29 | expect(fileContentMock).toMatchSnapshot();
|
30 | });
|
31 | });
|
32 |
|
33 | describe('when using js', () => {
|
34 | it('writes the story imports without types', () => {
|
35 | generate({ configPath: 'scripts/mocks/all-config-files', useJs: true });
|
36 | expect(pathMock).toEqual(
|
37 | path.resolve(__dirname, 'mocks/all-config-files/storybook.requires.js')
|
38 | );
|
39 | expect(fileContentMock).toMatchSnapshot();
|
40 | });
|
41 | });
|
42 |
|
43 | describe('when there are different file extensions', () => {
|
44 | it('writes the story imports', () => {
|
45 | generate({ configPath: 'scripts/mocks/file-extensions' });
|
46 | expect(pathMock).toEqual(
|
47 | path.resolve(__dirname, 'mocks/file-extensions/storybook.requires.ts')
|
48 | );
|
49 | expect(fileContentMock).toMatchSnapshot();
|
50 | });
|
51 | });
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | describe('when there is no story glob or addons', () => {
|
69 | it('throws an error', () => {
|
70 | expect(() => generate({ configPath: 'scripts/mocks/blank-config' })).toThrow();
|
71 | });
|
72 | });
|
73 |
|
74 | describe('when there is no preview', () => {
|
75 | it('does not add preview related stuff', () => {
|
76 | generate({ configPath: 'scripts/mocks/no-preview' });
|
77 | expect(pathMock).toEqual(path.resolve(__dirname, 'mocks/no-preview/storybook.requires.ts'));
|
78 | expect(fileContentMock).toMatchSnapshot();
|
79 | });
|
80 | });
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | describe('when there is a configuration object', () => {
|
96 | it('writes the story imports', () => {
|
97 | generate({ configPath: 'scripts/mocks/configuration-objects' });
|
98 | expect(pathMock).toEqual(
|
99 | path.resolve(__dirname, 'mocks/configuration-objects/storybook.requires.ts')
|
100 | );
|
101 | expect(fileContentMock).toMatchSnapshot();
|
102 | });
|
103 | });
|
104 | });
|
105 | });
|