1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var path_1 = require("path");
|
4 | var loader = require("./loader-impl");
|
5 | var helpers = require("../util/helpers");
|
6 | function getMockContext() {
|
7 | return {
|
8 | fileCache: getMockFileCache()
|
9 | };
|
10 | }
|
11 | function getMockFileCache() {
|
12 | return {
|
13 | get: function () { },
|
14 | set: function () { }
|
15 | };
|
16 | }
|
17 | function getMockWebpackObject(resourcePath) {
|
18 | return {
|
19 | cacheable: function () { },
|
20 | async: function () { },
|
21 | resourcePath: resourcePath
|
22 | };
|
23 | }
|
24 | describe('webpack loader', function () {
|
25 | it('should callback with file and original source map provided', function (done) {
|
26 |
|
27 | var mockContext = getMockContext();
|
28 | var mockSourceMap = {};
|
29 | var sourceString = 'sourceString';
|
30 | var fakePath = path_1.join(process.cwd(), 'some', 'path', 'content.js');
|
31 | var fakeContent = 'SomeFileContent';
|
32 | var mockWebpackObject = getMockWebpackObject(fakePath);
|
33 | var spy = jasmine.createSpy('mock webpack callback');
|
34 | spy.and.callFake(function () {
|
35 | assertFunction();
|
36 | });
|
37 | spyOn(mockWebpackObject, mockWebpackObject.cacheable.name);
|
38 | spyOn(mockWebpackObject, mockWebpackObject.async.name).and.returnValue(spy);
|
39 | spyOn(helpers, helpers.getContext.name).and.returnValue(mockContext);
|
40 | spyOn(helpers, helpers.readAndCacheFile.name).and.returnValue(Promise.resolve(fakeContent));
|
41 | spyOn(mockContext.fileCache, mockContext.fileCache.get.name).and.returnValue({
|
42 | path: fakePath,
|
43 | content: fakeContent
|
44 | });
|
45 |
|
46 | loader.webpackLoader(sourceString, mockSourceMap, mockWebpackObject);
|
47 |
|
48 | var assertFunction = function () {
|
49 | expect(helpers.readAndCacheFile).toHaveBeenCalledTimes(2);
|
50 | expect(spy).toHaveBeenCalledWith(null, fakeContent, mockSourceMap);
|
51 | done();
|
52 | };
|
53 | });
|
54 | it('should callback with file and map loaded from file cache', function (done) {
|
55 |
|
56 | var mockContext = getMockContext();
|
57 | var mockSourceMap = {};
|
58 | var sourceString = 'sourceString';
|
59 | var fakePath = path_1.join(process.cwd(), 'some', 'path', 'content.js');
|
60 | var fakeContent = "{\"test\": \"test\"}";
|
61 | var mockWebpackObject = getMockWebpackObject(fakePath);
|
62 | var spy = jasmine.createSpy('mock webpack callback');
|
63 | spy.and.callFake(function () {
|
64 | assertFunction();
|
65 | });
|
66 | spyOn(mockWebpackObject, mockWebpackObject.cacheable.name);
|
67 | spyOn(mockWebpackObject, mockWebpackObject.async.name).and.returnValue(spy);
|
68 | spyOn(helpers, helpers.getContext.name).and.returnValue(mockContext);
|
69 | spyOn(helpers, helpers.readAndCacheFile.name).and.returnValue(Promise.resolve(fakeContent));
|
70 | var fileCacheSpy = spyOn(mockContext.fileCache, mockContext.fileCache.get.name).and.returnValue({
|
71 | path: fakePath,
|
72 | content: fakeContent
|
73 | });
|
74 |
|
75 | loader.webpackLoader(sourceString, mockSourceMap, mockWebpackObject);
|
76 |
|
77 | var assertFunction = function () {
|
78 | expect(fileCacheSpy).toHaveBeenCalledTimes(2);
|
79 | expect(fileCacheSpy.calls.first().args[0]).toEqual(fakePath);
|
80 | expect(fileCacheSpy.calls.mostRecent().args[0]).toEqual(fakePath + '.map');
|
81 | expect(spy.calls.mostRecent().args[0]).toEqual(null);
|
82 | expect(spy.calls.mostRecent().args[1]).toEqual(fakeContent);
|
83 | expect(spy.calls.mostRecent().args[2]).not.toEqual(mockSourceMap);
|
84 | done();
|
85 | };
|
86 | });
|
87 | it('should callback with error when can\'t load file from disk', function (done) {
|
88 |
|
89 | var cantReadFileError = 'Failed to read file from disk';
|
90 | var mockContext = getMockContext();
|
91 | var mockSourceMap = {};
|
92 | var sourceString = 'sourceString';
|
93 | var fakePath = path_1.join(process.cwd(), 'some', 'path', 'content.js');
|
94 | var mockWebpackObject = getMockWebpackObject(fakePath);
|
95 | var spy = jasmine.createSpy('mock webpack callback');
|
96 | spy.and.callFake(function () {
|
97 | assertFunction();
|
98 | });
|
99 | spyOn(mockWebpackObject, mockWebpackObject.cacheable.name);
|
100 | spyOn(mockWebpackObject, mockWebpackObject.async.name).and.returnValue(spy);
|
101 | spyOn(helpers, helpers.getContext.name).and.returnValue(mockContext);
|
102 | spyOn(mockContext.fileCache, mockContext.fileCache.get.name).and.returnValue(null);
|
103 | spyOn(mockContext.fileCache, mockContext.fileCache.set.name);
|
104 | spyOn(helpers, helpers.readAndCacheFile.name).and.returnValue(Promise.reject(new Error(cantReadFileError)));
|
105 |
|
106 | var assertFunction = function () {
|
107 | expect(spy.calls.mostRecent().args[0]).toBeTruthy();
|
108 | expect(spy.calls.mostRecent().args[0].message).toEqual(cantReadFileError);
|
109 | done();
|
110 | };
|
111 |
|
112 | return loader.webpackLoader(sourceString, mockSourceMap, mockWebpackObject);
|
113 | });
|
114 | it('should callback with content from disk', function (done) {
|
115 |
|
116 | var mockContext = getMockContext();
|
117 | var mockSourceMap = {};
|
118 | var sourceString = 'sourceString';
|
119 | var fakePath = path_1.join(process.cwd(), 'some', 'path', 'content.js');
|
120 | var fakeContent = "{\"test\": \"test\"}";
|
121 | var mockWebpackObject = getMockWebpackObject(fakePath);
|
122 | var callbackSpy = jasmine.createSpy('mock webpack callback');
|
123 | callbackSpy.and.callFake(function () {
|
124 | assertFunction();
|
125 | });
|
126 | spyOn(mockWebpackObject, mockWebpackObject.cacheable.name);
|
127 | spyOn(mockWebpackObject, mockWebpackObject.async.name).and.returnValue(callbackSpy);
|
128 | spyOn(helpers, helpers.getContext.name).and.returnValue(mockContext);
|
129 | spyOn(mockContext.fileCache, mockContext.fileCache.set.name);
|
130 | var readFileSpy = spyOn(helpers, helpers.readAndCacheFile.name).and.returnValue(Promise.resolve(fakeContent));
|
131 | spyOn(mockContext.fileCache, mockContext.fileCache.get.name).and.returnValue({
|
132 | path: fakePath,
|
133 | content: fakeContent
|
134 | });
|
135 |
|
136 | loader.webpackLoader(sourceString, mockSourceMap, mockWebpackObject);
|
137 |
|
138 | var assertFunction = function () {
|
139 | expect(readFileSpy).toHaveBeenCalledTimes(2);
|
140 | expect(readFileSpy.calls.first().args[0]).toEqual(fakePath);
|
141 | expect(readFileSpy.calls.mostRecent().args[0]).toEqual(fakePath + '.map');
|
142 | expect(callbackSpy.calls.mostRecent().args[0]).toEqual(null);
|
143 | expect(callbackSpy.calls.mostRecent().args[1]).toEqual(fakeContent);
|
144 | expect(callbackSpy.calls.mostRecent().args[2]).toBeTruthy();
|
145 | done();
|
146 | };
|
147 | });
|
148 | });
|