insomnia/plugins/insomnia-plugin-file/__tests__/index.test.js

35 lines
972 B
JavaScript
Raw Normal View History

const path = require('path');
const tag = require('..').templateTags[0];
2018-06-25 17:42:50 +00:00
function assertTemplate(args, expected) {
return async function() {
const result = await tag.run(null, ...args);
expect(result).toBe(expected);
};
}
2018-06-25 17:42:50 +00:00
function assertTemplateFails(args, expected) {
return async function() {
try {
await tag.run(null, ...args);
fail(`Render should have thrown ${expected}`);
} catch (err) {
expect(err.message).toContain(expected);
}
};
}
describe('FileExtension', () => {
const filename = path.resolve(__dirname, path.join('./test.txt'));
const escaped = filename.replace(/\\/g, '\\\\');
it('reads from string', assertTemplate([escaped], 'Hello World!'));
2018-06-25 17:42:50 +00:00
it(
'fails on missing file',
assertTemplateFails(
[path.resolve('/foo')],
`ENOENT: no such file or directory, open '${path.resolve('/foo')}'`,
),
2018-06-25 17:42:50 +00:00
);
it('fails on no 2nd param', assertTemplateFails([], 'No file selected'));
});