salix/front/core/lib/specs/module-loader.spec.js

109 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-08-31 11:06:39 +00:00
describe('factory vnModuleLoader', () => {
let vnModuleLoader;
let $rootScope;
let $window;
2018-08-31 11:06:39 +00:00
beforeEach(ngModule('vnCore'));
2018-08-31 11:06:39 +00:00
2020-07-23 14:46:16 +00:00
beforeEach(inject((_vnModuleLoader_, _$rootScope_, $httpBackend, _$window_, $q) => {
2018-08-31 11:06:39 +00:00
vnModuleLoader = _vnModuleLoader_;
$rootScope = _$rootScope_;
$window = _$window_;
$window.validations = {};
$window.routes = [
{
module: 'myModule',
dependencies: ['fooModule', 'barModule']
}, {
module: 'fooModule',
dependencies: ['myModule']
}, {
module: 'barModule'
}
];
$httpBackend.whenGET('Schemas/modelInfo')
.respond({
FooModel: {
properties: {
id: {type: 'Number'},
email: {type: 'String'},
field: {type: 'Boolean'}
},
validations: {
id: [{
validation: 'presence'
}],
email: [{
validation: 'format',
with: '/@/'
}],
field: [{
validation: 'custom',
bindedFunction: '() => true'
}]
}
}
});
$httpBackend.flush();
vnModuleLoader.moduleImport = () => $q.resolve();
2018-08-31 11:06:39 +00:00
}));
describe('load()', () => {
it('should throw error if module does not exist', async() => {
let errorThrown;
2018-08-31 11:06:39 +00:00
vnModuleLoader.load('unexistentModule')
.catch(() => errorThrown = true);
$rootScope.$apply();
2018-08-31 11:06:39 +00:00
expect(errorThrown).toBeTruthy();
2018-08-31 11:06:39 +00:00
});
it('should set module loaded to true when it is loaded', async() => {
vnModuleLoader.load('barModule');
$rootScope.$apply();
expect(vnModuleLoader.loaded['barModule']).toBeTruthy();
});
it('should resolve returned promise when module is loaded', async() => {
let loaded;
vnModuleLoader.load('barModule')
.then(() => loaded = true);
$rootScope.$apply();
expect(loaded).toBeTruthy();
});
it('should load dependencies', async() => {
vnModuleLoader.load('fooModule');
$rootScope.$apply();
expect(vnModuleLoader.loaded['barModule']).toBeTruthy();
});
it('should work with circular dependencies', async() => {
vnModuleLoader.load('myModule');
$rootScope.$apply();
expect(vnModuleLoader.loaded['fooModule']).toBeTruthy();
});
it('should load models information and parse validations', async() => {
vnModuleLoader.load('barModule');
2018-08-31 11:06:39 +00:00
let FooModel = $window.validations.FooModel;
let validations = FooModel && FooModel.validations;
2018-08-31 11:06:39 +00:00
expect(FooModel).toBeDefined();
expect(validations).toBeDefined();
expect(validations.email[0].with).toBeInstanceOf(RegExp);
expect(validations.field[0].bindedFunction).toBeInstanceOf(Function);
2018-08-31 11:06:39 +00:00
});
});
});