2017-10-13 14:26:47 +00:00
|
|
|
import './multi-check.js';
|
2019-04-30 07:19:40 +00:00
|
|
|
import crudModel from 'core/mocks/crud-model';
|
2017-10-13 14:26:47 +00:00
|
|
|
|
2018-09-06 14:38:49 +00:00
|
|
|
describe('Component vnMultiCheck', () => {
|
2017-10-17 12:24:40 +00:00
|
|
|
let controller;
|
2018-09-06 14:38:49 +00:00
|
|
|
let $element;
|
2022-04-20 06:24:12 +00:00
|
|
|
let $httpBackend;
|
2022-05-10 10:02:50 +00:00
|
|
|
let $httpParamSerializer;
|
2017-10-13 14:26:47 +00:00
|
|
|
|
2019-10-24 22:53:53 +00:00
|
|
|
beforeEach(ngModule('vnCore'));
|
2017-10-13 14:26:47 +00:00
|
|
|
|
2022-05-10 10:02:50 +00:00
|
|
|
beforeEach(inject(($componentController, _$httpBackend_, _$httpParamSerializer_) => {
|
2022-04-20 06:24:12 +00:00
|
|
|
$httpBackend = _$httpBackend_;
|
2022-05-10 10:02:50 +00:00
|
|
|
$httpParamSerializer = _$httpParamSerializer_;
|
2018-09-06 14:38:49 +00:00
|
|
|
$element = angular.element(`<div class="shown"></div>`);
|
|
|
|
controller = $componentController('vnMultiCheck', {$element: $element});
|
2019-04-30 07:19:40 +00:00
|
|
|
controller._model = crudModel;
|
|
|
|
controller._model.data = [
|
|
|
|
{id: 1, name: 'My item 1'},
|
|
|
|
{id: 2, name: 'My item 2'},
|
|
|
|
{id: 3, name: 'My item 3'}
|
|
|
|
];
|
2017-10-13 14:26:47 +00:00
|
|
|
}));
|
|
|
|
|
2019-04-30 07:19:40 +00:00
|
|
|
describe('checked() setter', () => {
|
|
|
|
it(`should set controller _checked property with the argument received then call toggle()`, () => {
|
2020-02-26 12:22:52 +00:00
|
|
|
jest.spyOn(controller, 'toggle');
|
2019-04-30 07:19:40 +00:00
|
|
|
controller.checked = crudModel;
|
2017-10-13 14:26:47 +00:00
|
|
|
|
2019-04-30 07:19:40 +00:00
|
|
|
expect(controller._checked).toEqual(crudModel);
|
|
|
|
expect(controller.toggle).toHaveBeenCalledWith();
|
2017-10-13 14:26:47 +00:00
|
|
|
});
|
2022-04-20 06:24:12 +00:00
|
|
|
|
|
|
|
it(`should set checkedDummyCount to null`, () => {
|
|
|
|
jest.spyOn(controller, 'toggle');
|
|
|
|
controller.checkedDummyCount = 12;
|
|
|
|
controller.checked = null;
|
|
|
|
|
|
|
|
expect(controller.checkedDummyCount).toBeNull();
|
|
|
|
});
|
2017-10-13 14:26:47 +00:00
|
|
|
});
|
|
|
|
|
2019-04-30 07:19:40 +00:00
|
|
|
describe('toggle()', () => {
|
2018-09-06 14:38:49 +00:00
|
|
|
it(`should set checked property inside each existing element`, () => {
|
2019-04-30 07:19:40 +00:00
|
|
|
const data = controller.model.data;
|
|
|
|
|
|
|
|
expect(data[0].checked).not.toBeDefined();
|
|
|
|
expect(data[1].checked).not.toBeDefined();
|
|
|
|
expect(data[2].checked).not.toBeDefined();
|
|
|
|
|
|
|
|
controller._checked = true;
|
|
|
|
controller.checkAll = true;
|
|
|
|
controller.toggle();
|
|
|
|
|
|
|
|
expect(data[0].checked).toBeTruthy();
|
|
|
|
expect(data[1].checked).toBeTruthy();
|
|
|
|
expect(data[2].checked).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should unset checked property inside each existing element`, () => {
|
|
|
|
const data = controller.model.data;
|
|
|
|
data[0].checked = true;
|
|
|
|
data[1].checked = true;
|
|
|
|
data[2].checked = true;
|
|
|
|
|
|
|
|
controller._checked = false;
|
|
|
|
controller.checkAll = false;
|
|
|
|
controller.toggle();
|
|
|
|
|
|
|
|
expect(data[0].checked).toBeFalsy();
|
|
|
|
expect(data[1].checked).toBeFalsy();
|
|
|
|
expect(data[2].checked).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('areAllChecked()', () => {
|
2020-09-02 06:12:32 +00:00
|
|
|
it(`should return true if all elements are checked`, () => {
|
2019-04-30 07:19:40 +00:00
|
|
|
const data = controller.model.data;
|
|
|
|
data[0].checked = true;
|
|
|
|
data[1].checked = true;
|
|
|
|
data[2].checked = true;
|
|
|
|
|
|
|
|
expect(controller.areAllChecked()).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2020-09-02 06:12:32 +00:00
|
|
|
it(`should return false if not all elements are checked`, () => {
|
2019-04-30 07:19:40 +00:00
|
|
|
const data = controller.model.data;
|
|
|
|
data[0].checked = true;
|
|
|
|
data[1].checked = false;
|
|
|
|
data[2].checked = true;
|
|
|
|
|
|
|
|
expect(controller.areAllChecked()).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('areAllUnchecked()', () => {
|
2020-09-02 06:12:32 +00:00
|
|
|
it(`should return true if all elements are unchecked`, () => {
|
2019-04-30 07:19:40 +00:00
|
|
|
const data = controller.model.data;
|
|
|
|
data[0].checked = false;
|
|
|
|
data[1].checked = false;
|
|
|
|
data[2].checked = false;
|
|
|
|
|
|
|
|
expect(controller.areAllUnchecked()).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2020-09-02 06:12:32 +00:00
|
|
|
it(`should return false if not all elements are unchecked`, () => {
|
2019-04-30 07:19:40 +00:00
|
|
|
const data = controller.model.data;
|
|
|
|
data[0].checked = false;
|
|
|
|
data[1].checked = true;
|
|
|
|
data[2].checked = false;
|
|
|
|
|
|
|
|
expect(controller.areAllUnchecked()).toBeFalsy();
|
2017-10-13 14:43:11 +00:00
|
|
|
});
|
2017-10-20 08:42:55 +00:00
|
|
|
});
|
2020-09-02 06:12:32 +00:00
|
|
|
|
|
|
|
describe('setSelection()', () => {
|
|
|
|
it(`should check all elements between the index range`, () => {
|
|
|
|
controller.setSelection(true, 0, 2);
|
|
|
|
|
|
|
|
const data = controller.model.data;
|
|
|
|
const firstRow = data[0];
|
|
|
|
const secondRow = data[1];
|
|
|
|
const thirdRow = data[2];
|
|
|
|
|
|
|
|
expect(firstRow.checked).toBeTruthy();
|
|
|
|
expect(secondRow.checked).toBeTruthy();
|
|
|
|
expect(thirdRow.checked).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should uncheck all elements between the index range`, () => {
|
|
|
|
const data = controller.model.data;
|
|
|
|
const firstRow = data[0];
|
|
|
|
const secondRow = data[1];
|
|
|
|
const thirdRow = data[2];
|
|
|
|
|
|
|
|
firstRow.checked = true;
|
|
|
|
secondRow.checked = true;
|
|
|
|
thirdRow.checked = true;
|
|
|
|
|
|
|
|
controller.setSelection(false, 0, 1);
|
|
|
|
|
|
|
|
expect(firstRow.checked).toBeFalsy();
|
|
|
|
expect(secondRow.checked).toBeFalsy();
|
|
|
|
expect(thirdRow.checked).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
2022-04-20 06:24:12 +00:00
|
|
|
|
|
|
|
describe('countRows()', () => {
|
|
|
|
it(`should count visible rows and all rows of model`, () => {
|
|
|
|
controller.model.url = 'modelUrl/filter';
|
2022-05-10 10:02:50 +00:00
|
|
|
const data = controller.model.data;
|
|
|
|
const filter = {
|
|
|
|
limit: null
|
2022-04-20 06:24:12 +00:00
|
|
|
};
|
2022-05-10 10:02:50 +00:00
|
|
|
const serializedParams = $httpParamSerializer({filter});
|
2022-04-20 06:24:12 +00:00
|
|
|
|
2022-05-10 10:02:50 +00:00
|
|
|
const response = [
|
|
|
|
{id: 1, name: 'My item 1'},
|
|
|
|
{id: 2, name: 'My item 2'},
|
|
|
|
{id: 3, name: 'My item 3'},
|
|
|
|
{id: 4, name: 'My item 4'},
|
|
|
|
{id: 5, name: 'My item 5'},
|
|
|
|
{id: 6, name: 'My item 6'}
|
|
|
|
];
|
2022-04-20 06:24:12 +00:00
|
|
|
|
2022-05-10 10:02:50 +00:00
|
|
|
controller.countRows();
|
|
|
|
$httpBackend.expectGET(`modelUrl/filter?${serializedParams}`).respond(response);
|
|
|
|
$httpBackend.flush();
|
2022-04-20 06:24:12 +00:00
|
|
|
|
|
|
|
expect(controller.rows).toEqual(data.length);
|
2022-05-10 10:02:50 +00:00
|
|
|
expect(controller.allRowsCount).toEqual(response.length);
|
|
|
|
expect(controller.allRowsCount).toBeGreaterThan(controller.rows);
|
2022-04-20 06:24:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('checkDummy()', () => {
|
2022-05-10 10:02:50 +00:00
|
|
|
const allRows = 1234;
|
|
|
|
it(`should set the checked dummy count to all rows count if there was no count yet`, () => {
|
2022-04-20 06:24:12 +00:00
|
|
|
controller.checkedDummyCount = null;
|
2022-05-10 10:02:50 +00:00
|
|
|
controller.allRowsCount = allRows;
|
2022-04-20 06:24:12 +00:00
|
|
|
controller.checkDummy();
|
|
|
|
|
|
|
|
expect(controller.checkedDummyCount).toEqual(controller.allRowsCount);
|
|
|
|
});
|
|
|
|
|
2022-05-10 10:02:50 +00:00
|
|
|
it(`should remove the dummy count if there was an existing one`, () => {
|
|
|
|
controller.checkedDummyCount = allRows;
|
2022-04-20 06:24:12 +00:00
|
|
|
controller.checkDummy();
|
|
|
|
|
|
|
|
expect(controller.checkedDummyCount).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
2017-10-13 14:26:47 +00:00
|
|
|
});
|