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 pathloader-processor.spec.js
138 lines (108 loc) · 3.88 KB
/
loader-processor.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
128
129
130
131
132
133
134
135
136
137
138
/*jshint jasmine: true, node: true */
'use strict';
const mock = require('mock-require');
const logger = require('../utils/logger');
describe('SKY UX processor Webpack loader', () => {
const preloaderPath = '../loader/sky-processor/preload';
const postloaderPath = '../loader/sky-processor/postload';
const content = '<p>Hello, World!</p>';
let config;
beforeEach(() => {
config = {
resourcePath: '',
options: {
skyPagesConfig: {
skyux: {}
}
}
};
});
afterEach(() => {
mock.stop('my-plugin');
});
it('should not alter the file contents if plugins not present', () => {
const loader = require(preloaderPath);
const result = loader.call(config, content);
expect(result).toBe(content);
});
it('should handle invalid plugins', () => {
spyOn(logger, 'info');
config.options.skyPagesConfig.skyux.plugins = ['nonexistent-plugin'];
const loader = require(preloaderPath);
loader.apply(config, ['']);
expect(logger.info).toHaveBeenCalledWith(`Plugin not found: nonexistent-plugin`);
});
it('should pass file contents to the plugin to be altered', () => {
mock('my-plugin', {
preload: () => '<p></p>'
});
config.options.skyPagesConfig.skyux.plugins = ['my-plugin'];
const loader = require(preloaderPath);
const result = loader.call(config, content);
expect(result).toBe('<p></p>');
});
it('should not alter the content if the plugin doesn\'t return anything', () => {
mock('my-plugin', {
preload: () => {}
});
config.options.skyPagesConfig.skyux.plugins = ['my-plugin'];
const loader = require(preloaderPath);
const result = loader.call(config, content);
expect(result).toBe(content);
});
it('should not alter the content if the plugin\'s callbacks are invalid', () => {
mock('my-plugin', {
preload: 'foo'
});
config.options.skyPagesConfig.skyux.plugins = ['my-plugin'];
const loader = require(preloaderPath);
const result = loader.call(config, content);
expect(result).toBe(content);
});
it('should pass file contents to the plugin to be altered after', () => {
mock('my-plugin', {
preload: () => '<p></p>',
postload: (content) => content + '<br>'
});
config.options.skyPagesConfig.skyux.plugins = ['my-plugin'];
const preloader = require(preloaderPath);
const postloader = require(postloaderPath);
let result = preloader.call(config, content);
result = postloader.call(config, result);
expect(result).toBe('<p></p><br>');
});
it('should pass the resource path into the plugin', () => {
mock('my-plugin', {
postload: (content, resourcePath) => `<p>${resourcePath}</p>`
});
config.options.skyPagesConfig.skyux.plugins = ['my-plugin'];
const loader = require(postloaderPath);
const result = loader.call(config, content);
expect(result).toBe(`<p>${config.resourcePath}</p>`);
});
it('should pass skyPagesConfig into the plugin', () => {
mock('my-plugin', {
postload: (content, resourcePath, skyPagesConfig) => `<p>${skyPagesConfig.skyux.app.name}</p>`
});
config.options.skyPagesConfig.skyux.app = {
name: 'My App'
};
config.options.skyPagesConfig.skyux.plugins = ['my-plugin'];
const loader = require(postloaderPath);
const result = loader.call(config, content);
expect(result).toBe(`<p>${config.options.skyPagesConfig.skyux.app.name}</p>`);
});
it('should pass file contents to many plugins', () => {
mock('my-plugin', {
preload: () => '<p></p>'
});
mock('my-other-plugin', {
preload: (content) => `${content}<br>`
});
config.options.skyPagesConfig.skyux.plugins = ['my-plugin', 'my-other-plugin'];
const loader = require(preloaderPath);
const result = loader.call(config, content);
expect(result).toBe('<p></p><br>');
mock.stop('my-other-plugin');
});
});