This repository was archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathconfig-sky-pages.spec.js
127 lines (102 loc) · 3.9 KB
/
config-sky-pages.spec.js
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
/*jshint jasmine: true, node: true */
'use strict';
const fs = require('fs-extra');
const logger = require('../utils/logger');
describe('config sky-pages', () => {
it('should expose a getSkyPagesConfig method', () => {
const lib = require('../config/sky-pages/sky-pages.config');
expect(typeof lib.getSkyPagesConfig).toEqual('function');
});
it('should read name from skyuxconfig.json else package.json', () => {
const name = 'sky-pages-name';
const lib = require('../config/sky-pages/sky-pages.config');
const appBase = lib.getAppBase({
skyux: {
name: name,
mode: 'advanced'
}
});
expect(appBase).toEqual('/' + name + '/');
});
it('should load the config files that exist in order', () => {
const tempSpaReference = 'SPA_REFERENCE';
const readJsonSync = fs.readJsonSync;
const existsSync = fs.existsSync;
spyOn(logger, 'info');
spyOn(process, 'cwd').and.returnValue(tempSpaReference);
spyOn(fs, 'existsSync').and.callFake(filename => {
// Catch our package.json
if (filename.includes('package.json')) {
return true;
}
// Catch any skyuxconfig files
if (filename.includes('skyuxconfig.json') || filename.includes('skyuxconfig.build.json')) {
return true;
}
// Pass through normal behavior
return existsSync(filename);
});
spyOn(fs, 'readJsonSync').and.callFake((filename, encoding) => {
const isSpaDirectory = filename.includes(tempSpaReference);
const isDefaultConfig = filename.includes('skyuxconfig.json');
const isCommandConfig = filename.includes('skyuxconfig.build.json');
let config = {};
// Catch our package.json
if (filename.includes('package.json')) {
return JSON.stringify({
name: 'my-app-name'
});
}
// Catch our skyuxconfig files
if (isDefaultConfig || isCommandConfig) {
// Asking for builder's skyuxconfig.json
if (!isSpaDirectory && isDefaultConfig) {
config.a = 1; // Merged through entire process
config.z = 9; // Unique to this file
// Asking for builder's skyuxconfig.build.json
} else if (!isSpaDirectory && isCommandConfig) {
config.a = 2; // Merged through entire process
config.b = 1; // Starts here and merged up
config.y = 8; // Unique to this file
// Asking for SPA's skyuxconfig.json
} else if (isSpaDirectory && isDefaultConfig) {
config.a = 3; // Merged through entire process
config.b = 2;
config.c = 1; // Starts here and merged up
config.x = 7; // Unique to this file
// Asking for SPA's skyuxconfig.json
} else if (isSpaDirectory && isCommandConfig) {
config.a = 4; // Merged through entire process
config.b = 3;
config.c = 2;
config.w = 6; // Unique to this file
}
return config;
}
// Pass through normal behavior
return readJsonSync(filename, encoding);
});
const lib = require('../config/sky-pages/sky-pages.config');
const config = lib.getSkyPagesConfig('build').skyux;
expect(config.a).toEqual(4);
expect(config.b).toEqual(3);
expect(config.c).toEqual(2);
expect(config.w).toEqual(6);
expect(config.x).toEqual(7);
expect(config.y).toEqual(8);
expect(config.z).toEqual(9);
});
it('should handle config files that do not exist', () => {
spyOn(logger, 'info');
spyOn(fs, 'existsSync').and.returnValue(false);
spyOn(logger, 'error');
const command = 'build';
const lib = require('../config/sky-pages/sky-pages.config');
const config = lib.getSkyPagesConfig(command);
expect(config.skyux).toEqual({});
expect(config.runtime.command).toEqual(command);
expect(logger.error).toHaveBeenCalledWith(
'The `name` property should exist in package.json or skyuxconfig.json'
);
});
});