Skip to content

Commit 74756c1

Browse files
committed
chore(test): add test for template hook
Signed-off-by: Tobias Gurtzick <[email protected]>
1 parent 0de35ee commit 74756c1

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

lib/template.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ const File = require('./file.js');
44
const { util: dbmUtil, log } = require('db-migrate-shared');
55

66
const Template = function () {
7-
this.plugins = arguments[4];
8-
delete arguments[4];
7+
if (arguments[4] && typeof arguments[4].overwrite === 'function') {
8+
this.plugins = arguments[4];
9+
arguments[4] = undefined;
10+
arguments.length--;
11+
}
912
this.templateType = arguments[3];
1013
// this is a tweak for node 4 this can be translated
1114
// to ...arguments instead in upcoming node versions

test/migration_test.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,32 @@ function getTemplate () {
234234
lab.experiment('when template plugin is set', function () {
235235
lab.experiment('as sql file loader', function () {
236236
const name = 'test';
237-
const plugins = Plugins.createSinglePlugin(
238-
`template:overwrite:provider:${name}`,
239-
opts => {
240-
return `test all variables`;
241-
}
242-
);
243-
const migration = new Template(fileName, dirName, date, name, plugins);
244237

245238
lab.test('should return sql file loader template', () => {
246-
const actual = migration.getTemplate();
247-
Code.expect(actual).to.equal(`test all variables`);
239+
const plugins = Plugins.createSinglePlugin(
240+
`template:overwrite:provider:${name}`,
241+
opts => {
242+
return `test all variables`;
243+
}
244+
);
245+
const migration = new Template(fileName, dirName, date, name, plugins);
246+
247+
Code.expect(migration.getTemplate()).to.equal(`test all variables`);
248+
});
249+
250+
lab.test('should throw if plugin fails', () => {
251+
const plugins = Plugins.createSinglePlugin(
252+
`template:overwrite:provider:${name}`,
253+
opts => {
254+
throw new Error('test');
255+
}
256+
);
257+
const migration = new Template(fileName, dirName, date, name, plugins);
258+
259+
Code.expect(migration.getTemplate.bind(migration)).to.throw(
260+
Error,
261+
'test'
262+
);
248263
});
249264
});
250265

@@ -263,6 +278,23 @@ function getTemplate () {
263278
});
264279
});
265280

281+
lab.experiment('as sql default ignore on init template', function () {
282+
const migration = new Template(
283+
fileName,
284+
dirName,
285+
date,
286+
Template.TemplateType.SQL_FILE_LOADER_IGNORE_ON_INIT,
287+
internals
288+
);
289+
290+
lab.test('should return sql ignore on init template', () => {
291+
const actual = migration.getTemplate();
292+
Code.expect(actual).to.equal(
293+
migration.sqlFileLoaderIgnoreOnInitTemplate()
294+
);
295+
});
296+
});
297+
266298
lab.experiment('as default coffee', function () {
267299
const migration = new Template(
268300
fileName,

0 commit comments

Comments
 (0)