Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 042efb5

Browse files
committed
test(generators): improve and add new tests
1 parent fb03df5 commit 042efb5

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/generators/util.spec.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as helpers from '../util/helpers';
55
import * as globUtils from '../util/glob-util';
66
import * as util from './util';
77
import * as GeneratorConstants from './constants';
8+
import * as TypeScriptUtils from '../util/typescript-utils';
89

910
describe('util', () => {
1011
describe('hydrateRequest', () => {
@@ -406,13 +407,26 @@ $TAB_CONTENT
406407
});
407408

408409
it('should return a succesful promise', () => {
409-
let rejected = false;
410+
// set up spies
411+
spyOn(helpers, helpers.readFileAsync.name).and.returnValue(Promise.resolve('file content'));
412+
spyOn(fs, 'readdirSync').and.returnValue([
413+
'/path/to/nowhere',
414+
'path/to/somewhere'
415+
]);
416+
spyOn(helpers, helpers.writeFileAsync.name).and.returnValue(Promise.resolve());
417+
spyOn(helpers, helpers.mkDirpAsync.name).and.returnValue(Promise.resolve());
418+
spyOn(TypeScriptUtils, TypeScriptUtils.insertNamedImportIfNeeded.name).and.returnValue('file content');
419+
spyOn(TypeScriptUtils, TypeScriptUtils.appendNgModuleDeclaration.name).and.returnValue('sliced string');
410420

411-
util.nonPageFileManipulation(context, 'coolStuff', '/src/pages/cool-tab-one/cool-tab-one.module.ts', 'pipe').catch(() => {
412-
rejected = true;
413-
});
421+
// what we want to test
422+
const promise = util.nonPageFileManipulation(context, 'coolStuff', '/src/pages/cool-tab-one/cool-tab-one.module.ts', 'pipe');
414423

415-
expect(rejected).toBeFalsy();
424+
// test
425+
return promise.then((value: string[]) => {
426+
expect(helpers.readFileAsync).toHaveBeenCalled();
427+
expect(helpers.writeFileAsync).toHaveBeenCalled();
428+
expect(helpers.mkDirpAsync).toHaveBeenCalled();
429+
});
416430
});
417431

418432
it('should throw when files are not written succesfully', () => {

src/util/typescript-utils.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,29 @@ export class TacoBellModule {}
121121
}
122122
});
123123
});
124+
125+
describe('insertNamedImportIfNeeded', () => {
126+
it('should return modified file content, which is a string', () => {
127+
const filePath = '/path/to/my/file';
128+
const fileContent = 'import {A, B, C} from modulePath';
129+
const namedImport = 'NamedImport';
130+
const fromModule = 'CoolModule';
131+
132+
const result = tsUtils.insertNamedImportIfNeeded(filePath, fileContent, namedImport, fromModule);
133+
134+
// TODO: figure out how to match the exact string
135+
expect(result).toEqual(jasmine.any(String));
136+
});
137+
});
138+
139+
it('should return the same file content as the import is already in the file', () => {
140+
const filePath = '/path/to/my/file';
141+
const fileContent = 'import { A } from "modulePath"';
142+
const namedImport = 'A';
143+
const fromModule = `modulePath`;
144+
145+
const result = tsUtils.insertNamedImportIfNeeded(filePath, fileContent, namedImport, fromModule);
146+
147+
expect(result).toEqual(fileContent);
148+
});
124149
});

0 commit comments

Comments
 (0)