2020-07-01 00:34:02 +00:00
|
|
|
import { InvalidArgumentError, NotFoundError } from '@apextoaster/js-utils';
|
2019-11-13 14:20:34 +00:00
|
|
|
import { expect } from 'chai';
|
2021-03-27 23:34:47 +00:00
|
|
|
import { DEFAULT_SCHEMA } from 'js-yaml';
|
2019-11-13 14:20:34 +00:00
|
|
|
import { join } from 'path';
|
|
|
|
|
2022-10-08 20:56:30 +00:00
|
|
|
import { createInclude, IncludeOptions } from '../../src/type/Include.js';
|
2019-11-13 14:20:34 +00:00
|
|
|
|
|
|
|
const TEST_ROOT = '../test/type';
|
|
|
|
|
2020-08-09 01:01:34 +00:00
|
|
|
const TEST_OPTIONS: IncludeOptions = {
|
|
|
|
exists: () => true,
|
|
|
|
join: (...path) => path.join('/'),
|
|
|
|
read: () => 'test',
|
|
|
|
resolve: (path: string) => path,
|
2021-03-27 23:34:47 +00:00
|
|
|
schema: DEFAULT_SCHEMA,
|
2020-07-01 00:34:02 +00:00
|
|
|
};
|
|
|
|
|
2020-03-29 13:13:57 +00:00
|
|
|
describe('include config type', async () => {
|
|
|
|
it('should resolve existing files', async () => {
|
2021-03-28 00:31:44 +00:00
|
|
|
const {includeType} = createInclude(TEST_OPTIONS);
|
2019-11-13 14:20:34 +00:00
|
|
|
expect(includeType.resolve(join(TEST_ROOT, 'include.yml'))).to.equal(true);
|
|
|
|
});
|
|
|
|
|
2020-03-29 13:13:57 +00:00
|
|
|
it('should throw when resolving missing files', async () => {
|
2021-03-28 00:31:44 +00:00
|
|
|
const {includeType} = createInclude({
|
2020-08-09 01:01:34 +00:00
|
|
|
...TEST_OPTIONS,
|
|
|
|
resolve: () => {
|
|
|
|
throw new NotFoundError();
|
|
|
|
},
|
|
|
|
});
|
2020-07-01 00:34:02 +00:00
|
|
|
|
2019-11-13 14:20:34 +00:00
|
|
|
expect(() => {
|
|
|
|
includeType.resolve(join(TEST_ROOT, 'missing.yml'));
|
|
|
|
}).to.throw(NotFoundError);
|
|
|
|
});
|
|
|
|
|
2020-03-29 13:13:57 +00:00
|
|
|
it('should construct data from file', async () => {
|
2021-03-28 00:31:44 +00:00
|
|
|
const {includeType} = createInclude(TEST_OPTIONS);
|
2019-11-13 14:20:34 +00:00
|
|
|
expect(includeType.construct(join(TEST_ROOT, 'include.yml'))).to.equal('test');
|
|
|
|
});
|
|
|
|
|
2020-03-29 13:13:57 +00:00
|
|
|
it('should throw when constructing missing files', async () => {
|
2021-03-28 00:31:44 +00:00
|
|
|
const {includeType} = createInclude({
|
2020-08-09 01:01:34 +00:00
|
|
|
...TEST_OPTIONS,
|
|
|
|
read: () => {
|
|
|
|
throw new InvalidArgumentError();
|
|
|
|
},
|
|
|
|
});
|
2020-07-01 00:34:02 +00:00
|
|
|
|
2019-11-13 14:20:34 +00:00
|
|
|
expect(() => {
|
|
|
|
includeType.construct(join(TEST_ROOT, 'missing.yml'));
|
|
|
|
}).to.throw(InvalidArgumentError);
|
|
|
|
});
|
|
|
|
});
|