2018-08-31 11:06:39 +00:00
|
|
|
describe('factory vnModuleLoader', () => {
|
|
|
|
let vnModuleLoader;
|
2019-11-01 12:02:47 +00:00
|
|
|
let $rootScope;
|
|
|
|
let $window;
|
2018-08-31 11:06:39 +00:00
|
|
|
|
2019-10-24 22:53:53 +00:00
|
|
|
beforeEach(ngModule('vnCore'));
|
2018-08-31 11:06:39 +00:00
|
|
|
|
2019-11-01 12:02:47 +00:00
|
|
|
beforeEach(angular.mock.inject((_vnModuleLoader_, _$rootScope_, $httpBackend, _$window_, $q) => {
|
2018-08-31 11:06:39 +00:00
|
|
|
vnModuleLoader = _vnModuleLoader_;
|
2019-11-01 12:02:47 +00:00
|
|
|
$rootScope = _$rootScope_;
|
|
|
|
$window = _$window_;
|
|
|
|
|
|
|
|
$window.validations = {};
|
|
|
|
$window.routes = [
|
|
|
|
{
|
|
|
|
module: 'myModule',
|
|
|
|
dependencies: ['fooModule', 'barModule']
|
|
|
|
}, {
|
|
|
|
module: 'fooModule',
|
|
|
|
dependencies: ['myModule']
|
|
|
|
}, {
|
|
|
|
module: 'barModule'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2020-05-21 07:23:44 +00:00
|
|
|
$httpBackend.whenGET('Schemas/modelInfo')
|
2019-11-01 12:02:47 +00:00
|
|
|
.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()', () => {
|
2019-11-01 12:02:47 +00:00
|
|
|
it('should throw error if module does not exist', async() => {
|
|
|
|
let errorThrown;
|
2018-08-31 11:06:39 +00:00
|
|
|
|
2019-11-01 12:02:47 +00:00
|
|
|
vnModuleLoader.load('unexistentModule')
|
|
|
|
.catch(() => errorThrown = true);
|
|
|
|
$rootScope.$apply();
|
2018-08-31 11:06:39 +00:00
|
|
|
|
2019-11-01 12:02:47 +00:00
|
|
|
expect(errorThrown).toBeTruthy();
|
2018-08-31 11:06:39 +00:00
|
|
|
});
|
|
|
|
|
2019-11-01 12:02:47 +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
|
|
|
|
2019-11-01 12:02:47 +00:00
|
|
|
let FooModel = $window.validations.FooModel;
|
|
|
|
let validations = FooModel && FooModel.validations;
|
2018-08-31 11:06:39 +00:00
|
|
|
|
2019-11-01 12:02:47 +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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|