forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-example.spec.mjs
135 lines (119 loc) · 4.41 KB
/
create-example.spec.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import path from 'canonical-path';
import fs from 'fs-extra';
import glob from 'glob';
import {EXAMPLE_CONFIG_FILENAME, STACKBLITZ_CONFIG_FILENAME} from './constants.mjs';
import {
copyExampleFiles,
createEmptyExample,
ensureExamplePath,
titleize,
writeExampleConfigFile,
writeStackBlitzFile,
} from './create-example.mjs';
describe('create-example tool', () => {
describe('createEmptyExample', () => {
it('should create an empty example with marker files', () => {
spyOn(fs, 'existsSync').and.returnValue(false);
spyOn(fs, 'ensureDirSync');
const writeFileSpy = spyOn(fs, 'writeFileSync');
createEmptyExample('foo-bar', '/path/to/foo-bar');
expect(writeFileSpy).toHaveBeenCalledTimes(2);
expect(writeFileSpy).toHaveBeenCalledWith(
path.resolve(`/path/to/foo-bar/${EXAMPLE_CONFIG_FILENAME}`),
jasmine.any(String)
);
expect(writeFileSpy).toHaveBeenCalledWith(
path.resolve(`/path/to/foo-bar/${STACKBLITZ_CONFIG_FILENAME}`),
jasmine.any(String)
);
});
it('should fail if the example name contains spaces', () => {
expect(() => createEmptyExample('foo bar', '/path/to/foo-bar')).toThrowError(
`Unable to create example. The example name contains spaces: 'foo bar'`
);
});
});
describe('ensureExamplePath', () => {
it('should error if the path already exists', () => {
spyOn(fs, 'existsSync').and.returnValue(true);
expect(() => ensureExamplePath('foo/bar')).toThrowError(
`Unable to create example. The path to the new example already exists: foo/bar`
);
});
it('should create the directory on disk', () => {
spyOn(fs, 'existsSync').and.returnValue(false);
const spy = spyOn(fs, 'ensureDirSync');
ensureExamplePath('foo/bar');
expect(spy).toHaveBeenCalledWith('foo/bar');
});
});
describe('writeExampleConfigFile', () => {
it('should write a JSON file to disk', () => {
const spy = spyOn(fs, 'writeFileSync');
writeExampleConfigFile('/foo/bar');
expect(spy).toHaveBeenCalledWith(path.resolve(`/foo/bar/${EXAMPLE_CONFIG_FILENAME}`), '');
});
});
describe('writeStackBlitzFile', () => {
it('should write a JSON file to disk', () => {
const spy = spyOn(fs, 'writeFileSync');
writeStackBlitzFile('bar-bar', '/foo/bar-bar');
expect(spy).toHaveBeenCalledWith(
path.resolve(`/foo/bar-bar/${STACKBLITZ_CONFIG_FILENAME}`),
[
'{',
' "description": "Bar Bar",',
' "files": [',
' "!**/*.d.ts",',
' "!**/*.js",',
' "!**/*.[1,2].*"',
' ],',
' "tags": [',
' [',
' "bar",',
' "bar"',
' ]',
' ]',
'}',
'',
].join('\n')
);
});
});
describe('copyExampleFiles', () => {
it('should copy over files that are not ignored by git', () => {
const sourceGitIgnorePath = path.resolve('/source/path', '.gitignore');
spyOn(console, 'log');
spyOn(fs, 'existsSync').and.returnValue(true);
const readFileSyncSpy = spyOn(fs, 'readFileSync').and.callFake((p) => {
switch (p) {
case sourceGitIgnorePath:
return '**/*.bad';
default:
throw new Error('Unexpected path');
}
});
spyOn(glob, 'sync').and.returnValue(['a/', 'a/b/', 'a/c', 'x.ts', 'x.bad']);
const ensureDirSyncSpy = spyOn(fs, 'ensureDirSync');
const copySyncSpy = spyOn(fs, 'copySync');
copyExampleFiles('/source/path', '/path/to/test-example', 'test-example');
expect(readFileSyncSpy).toHaveBeenCalledWith(sourceGitIgnorePath, 'utf8');
expect(ensureDirSyncSpy.calls.allArgs()).toEqual([
[path.resolve('/path/to/test-example/a')],
[path.resolve('/path/to/test-example')],
]);
expect(copySyncSpy.calls.allArgs()).toEqual([
[path.resolve('/source/path/a/c'), path.resolve('/path/to/test-example/a/c')],
[path.resolve('/source/path/x.ts'), path.resolve('/path/to/test-example/x.ts')],
]);
});
});
describe('titleize', () => {
it('should convert a kebab-case string to title-case', () => {
expect(titleize('abc')).toEqual('Abc');
expect(titleize('abc-def')).toEqual('Abc Def');
expect(titleize('123')).toEqual('123');
expect(titleize('abc---def')).toEqual('Abc - Def');
});
});
});