| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- describe('Core.getSourceDataAtCell', () => {
- var id = 'testContainer';
- beforeEach(function() {
- this.$container = $(`<div id="${id}"></div>`).appendTo('body');
- });
- afterEach(function() {
- if (this.$container) {
- destroy();
- this.$container.remove();
- }
- });
- it('should return null when is call without arguments', () => {
- handsontable({
- data: [[1, 2, 3], ['a', 'b', 'c']],
- });
- expect(getSourceDataAtCell()).toBeNull();
- });
- it('should return cell value when provided data was an array of arrays', () => {
- handsontable({
- data: [[1, 2, 3], ['a', 'b', 'c']],
- });
- expect(getSourceDataAtCell(1, 1)).toEqual('b');
- });
- it('should return cell value when provided data was an array of objects', () => {
- handsontable({
- data: [{a: 1, b: 2, c: 3}, {a: 'a', b: 'b', c: 'c'}],
- copyable: true
- });
- expect(getSourceDataAtCell(1, 'b')).toEqual('b');
- });
- it('should return cell value when provided data was an array of objects (nested structure)', () => {
- handsontable({
- data: [{a: 1, b: {a: 21, b: 22}, c: 3}, {a: 'a', b: {a: 'ba', b: 'bb'}, c: 'c'}],
- columns: [
- {data: 'a'},
- {data: 'b.a'},
- {data: 'b.b'},
- {data: 'c'},
- ]
- });
- expect(getSourceDataAtCell(1, 'b.b')).toEqual('bb');
- });
- it('should return cell value when data is provided by dataSchema', () => {
- handsontable({
- data: [
- model({id: 1, name: 'Ted Right', address: ''}),
- model({id: 2, name: 'Frank Honest', address: ''}),
- model({id: 3, name: 'Joan Well', address: ''}),
- model({id: 4, name: 'Gail Polite', address: ''}),
- model({id: 5, name: 'Michael Fair', address: ''})
- ],
- dataSchema: model,
- columns: [
- {data: property('id')},
- {data: property('name')},
- {data: property('address')}
- ]
- });
- function model(opts) {
- var
- _pub = {},
- _priv = {
- id: undefined,
- name: undefined,
- address: undefined
- };
- for (var i in opts) {
- if (Object.prototype.hasOwnProperty.call(opts, i)) {
- _priv[i] = opts[i];
- }
- }
- _pub.attr = function(attr, val) {
- if (typeof val === 'undefined') {
- return _priv[attr];
- }
- _priv[attr] = val;
- return _pub;
- };
- return _pub;
- }
- function property(attr) {
- return function(row, value) {
- return row.attr(attr, value);
- };
- }
- expect(getSourceDataAtCell(1, 1)).toEqual('Frank Honest');
- });
- describe('`modifyRowData` hook', () => {
- it('should be possible to change data for row on the fly ', () => {
- handsontable({
- data: [
- ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
- ['2008', 10, 11, 12, 13],
- ['2009', 20, 11, 14, 13],
- ['2010', 30, 15, 12, 13]
- ],
- modifyRowData(row) {
- var newDataset = [];
- if (row === 1) {
- newDataset.push('2016', 0, 0, 0, 0);
- }
- return newDataset.length ? newDataset : void 0;
- }
- });
- expect(getSourceDataAtCell(1, 0)).toEqual('2016');
- });
- });
- });
|