describe('Component smartTable', () => { let $element; let controller; let $httpBackend; beforeEach(ngModule('vnCore')); beforeEach(inject(($compile, $rootScope, _$httpBackend_) => { $httpBackend = _$httpBackend_; $element = $compile(``)($rootScope); controller = $element.controller('smartTable'); })); afterEach(() => { $element.remove(); }); describe('options setter()', () => { it(`should throw an error if the table doesn't have an identifier`, () => { const options = {activeButtons: {shownColumns: []}}; expect(() => controller.options = options).toThrowError(/View identifier not defined/); }); it('should not throw an error if the table does have an identifier', () => { const options = {activeButtons: {shownColumns: []}}; controller.viewConfigId = 'test'; expect(() => controller.options = options).not.toThrow(); }); }); describe('getDefaultViewConfig()', () => { it('should perform a query and return the default view columns', done => { const expectedResponse = [{ columns: {} }]; $httpBackend.expectGET('DefaultViewConfigs').respond(expectedResponse); controller.getDefaultViewConfig().then(columns => { expect(columns).toEqual(expectedResponse[0].columns); done(); }).catch(done.fail); $httpBackend.flush(); }); }); describe('viewConfig setter', () => { it('should just call applyViewConfig() if a viewConfig was provided', () => { spyOn(controller, 'applyViewConfig'); controller.viewConfig = [{}]; expect(controller.applyViewConfig).toHaveBeenCalled(); }); it('should not get a defaultConfig then insert a new one', () => { spyOn(controller, 'applyViewConfig'); controller.$.userViewModel = { insert: jest.fn() }; const emptyResponse = [{ columns: {} }]; controller.columns = [ {field: 'test1'}, {field: 'test2'} ]; $httpBackend.expectGET('DefaultViewConfigs').respond(emptyResponse); controller.viewConfig = []; $httpBackend.flush(); const expectedObject = {configuration: {'test1': true, 'test2': true}}; expect(controller.$.userViewModel.insert).toHaveBeenCalledWith(expect.objectContaining(expectedObject)); expect(controller.applyViewConfig).toHaveBeenCalled(); }); }); describe('applyViewConfig()', () => { it('should ', () => { controller.$.userViewModel = { viewConfig: {'test1': true, 'test2': false} }; controller._columns = [ {field: 'test1'}, {field: 'test2'} ]; controller.applyViewConfig(); }); }); });