UNPKG

3.71 kBJavaScriptView Raw
1const path = require('path');
2const { generate } = require('./generate');
3
4let pathMock;
5let fileContentMock;
6
7jest.mock('fs', () => ({
8 ...jest.requireActual('fs'),
9 writeFileSync: (filePath, fileContent, opts) => {
10 pathMock = filePath;
11 fileContentMock = fileContent;
12 },
13}));
14
15jest.mock('prettier', () => ({
16 format(s, opts) {
17 return s;
18 },
19}));
20
21describe('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 // TODO can we support exclude globs?
54 // describe('when there is a story glob and exclude paths globs', () => {
55 // it('writes the story imports', () => {
56 // generate({ configPath: 'scripts/mocks/exclude-config-files' });
57 // expect(pathMock).toEqual(
58 // path.resolve(__dirname, 'mocks/exclude-config-files/storybook.requires.ts')
59 // );
60
61 // expect(fileContentMock).toContain('include-components/FakeStory.stories.tsx');
62 // expect(fileContentMock).not.toContain('exclude-components/FakeStory.stories.tsx');
63
64 // expect(fileContentMock).toMatchSnapshot();
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 // TODO does this still make sense?
83 // describe('when the absolute option is true', () => {
84 // it('should write absolute paths to the requires file', () => {
85 // generate({ configPath: 'scripts/mocks/all-config-files', absolute: true });
86 // expect(pathMock).toEqual(
87 // path.resolve(__dirname, 'mocks/all-config-files/storybook.requires.ts')
88 // );
89
90 // // expect(fileContentMock).toContain(`FakeStory.stories.tsx`);
91 // expect(fileContentMock).toContain(path.resolve(__dirname, 'mocks/all-config-files'));
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});