Skip to content

Commit

Permalink
Fix total on addNewRow and dismissEditor (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanderslice authored and bencripps committed Mar 6, 2017
1 parent 0ecf836 commit 01830cf
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 9 deletions.
29 changes: 22 additions & 7 deletions src/reducers/actionHelpers/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,17 @@ export const dismissEditor = (state, { stateKey }) => {
if (previousData
&& previousProxy
&& previousData.size > previousProxy.size) {
previousTotal = previousProxy.size;
previousTotal = previousTotal - 1;
}

const record = state.get(stateKey);

if (record) {
const updated = record.merge({
data: previousProxy,
proxy: previousProxy,
currentRecords: previousProxy,
total: previousTotal,
isEditing: false,
total: previousTotal,
lastUpdate: generateLastUpdate()
});

Expand All @@ -96,16 +95,26 @@ export const dismissEditor = (state, { stateKey }) => {
};

export const removeRow = (state, { stateKey, rowIndex }) => {
const existingState = state.get(stateKey);
const currentTotal = existingState.get('total');

const remainingRows = state
.getIn([stateKey, 'data'])
.remove(rowIndex || 0, 1);

const record = state.get(stateKey);

const updatedTotal = existingState
&& currentTotal
&& currentTotal > 0
? currentTotal - 1
: 0;

const updated = record.merge({
data: remainingRows,
proxy: remainingRows,
currentRecords: remainingRows,
total: updatedTotal,
lastUpdate: generateLastUpdate()
});

Expand Down Expand Up @@ -160,17 +169,23 @@ export const addNewRow = (state, { rowId, stateKey, rowIndex }) => {
data = new List();
}

rowIndex = rowIndex ===undefined
const updatedTotal = existingState
&& existingState.get('total')
? existingState.get('total')
: 0;

rowIndex = rowIndex === undefined
? 0
: rowIndex;

const newData = data.insert(rowIndex, newRow);

const updated = existingState.merge({
data: newData,
proxy: data,
isEditing: true,
lastUpdate: generateLastUpdate(),
total: newData.size
total: updatedTotal + 1,
lastUpdate: generateLastUpdate()
});

return state.setIn([stateKey], updated);
Expand Down Expand Up @@ -245,7 +260,7 @@ export const setTreeNodeVisibility = (state, {
return state.setIn([stateKey], updated);
};

export const saveRow = (state, { rowIndex, stateKey, values}) => {
export const saveRow = (state, { rowIndex, stateKey, values }) => {
const data = state
.getIn([stateKey, 'data'])
.set(rowIndex, fromJS(values));
Expand Down
235 changes: 233 additions & 2 deletions test/reducers/components/datasource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,54 @@ describe('The grid dataSource reducer dissmissEditor func', () => {
dataSource(inState, action)
).toEqualImmutable(outState);
});

it('Should decrement and maintain total if proxy less values than data',
() => {
const inState = fromJS({
'test-grid': {
data: [
{ cell: 1 },
{ cell: 2 },
{ cell: 3 }

],
proxy: [
{ cell: 1 },
{ cell: 2 }
],
total: 151
}
});

const outState = fromJS({
'test-grid': {
proxy: [
{ cell: 1 },
{ cell: 2 }
],
total: 150,
data: [
{ cell: 1 },
{ cell: 2 }
],
currentRecords: [
{ cell: 1 },
{ cell: 2 }
],
isEditing: false,
lastUpdate: 1
}
});

const action = {
stateKey: 'test-grid',
type: DISMISS_EDITOR
};

expect(
dataSource(inState, action)
).toEqualImmutable(outState);
});
});

describe('The grid dataSource reducer removeRow func', () => {
Expand Down Expand Up @@ -260,7 +308,7 @@ describe('The grid dataSource reducer removeRow func', () => {
currentRecords: [
{ cell: 2 }
],
total: 2,
total: 1,
lastUpdate: 1
}
});
Expand Down Expand Up @@ -288,7 +336,7 @@ describe('The grid dataSource reducer removeRow func', () => {
currentRecords: [
{ cell: 1 }
],
total: 2,
total: 1,
lastUpdate: 1
}
});
Expand All @@ -305,6 +353,107 @@ describe('The grid dataSource reducer removeRow func', () => {

});

it('Should default total to 0 when inState total is 0', () => {

const customInState = fromJS({
'test-grid': {
proxy: [],
data: [],
currentRecords: [],
total: 0,
lastUpdate: 1
}
});

const outState = fromJS({
'test-grid': {
proxy: [],
data: [],
currentRecords: [],
total: 0,
lastUpdate: 1
}
});

const action = {
stateKey: 'test-grid',
rowIndex: 1,
type: REMOVE_ROW
};

expect(
dataSource(customInState, action)
).toEqualImmutable(outState);

});

it('Should default total to 0 when total not provided', () => {

const customInState = fromJS({
'test-grid': {
proxy: [],
data: [],
currentRecords: [],
lastUpdate: 1
}
});

const outState = fromJS({
'test-grid': {
proxy: [],
data: [],
currentRecords: [],
total: 0,
lastUpdate: 1
}
});

const action = {
stateKey: 'test-grid',
rowIndex: 1,
type: REMOVE_ROW
};

expect(
dataSource(customInState, action)
).toEqualImmutable(outState);

});

it('Should default total to 0 when total provided is negative', () => {

const customInState = fromJS({
'test-grid': {
proxy: [],
data: [],
currentRecords: [],
total: -1,
lastUpdate: 1
}
});

const outState = fromJS({
'test-grid': {
proxy: [],
data: [],
currentRecords: [],
total: 0,
lastUpdate: 1
}
});

const action = {
stateKey: 'test-grid',
rowIndex: 1,
type: REMOVE_ROW
};

expect(
dataSource(customInState, action)
).toEqualImmutable(outState);

});

});

describe('The grid dataSource reducer addRow func', () => {
Expand Down Expand Up @@ -362,6 +511,88 @@ describe('The grid dataSource reducer addRow func', () => {
).toEqualImmutable(outState);
});

it('Should add a new row and maintain total if multiple pages', () => {
const customInState = fromJS({
'test-grid': {
proxy: [
{ cell: 1 },
{ cell: 2 }
],
data: [
{ cell: 1 },
{ cell: 2 }
],
currentRecords: [
{ cell: 1 },
{ cell: 2 }
],
total: 150
}
});

const outState = fromJS({
'test-grid': {
proxy: [
{ cell: 1 },
{ cell: 2 }
],
data: [
{ cell: '', _key: 'row-0' },
{ cell: 1 },
{ cell: 2 }
],
currentRecords: [
{ cell: 1 },
{ cell: 2 }
],
total: 151,
isEditing: true,
lastUpdate: 1
}
});

const action = {
stateKey: 'test-grid',
rowId: 'row-0',
type: ADD_NEW_ROW
};

expect(
dataSource(customInState, action)
).toEqualImmutable(outState);
});

it('Should add a new row and + 1 to total when no total on inState', () => {
const customInState = fromJS({
'test-grid': {
proxy: [],
data: [],
currentRecords: []
}
});

const outState = fromJS({
'test-grid': {
proxy: [],
data: [{_key: 'row-0'}],
currentRecords: [],
total: 1,
isEditing: true,
lastUpdate: 1
}
});

const action = {
stateKey: 'test-grid',
rowId: 'row-0',
type: ADD_NEW_ROW
};

expect(
dataSource(customInState, action)
).toEqualImmutable(outState);
});

it('Should add a new blank row if no rows have been established', () => {
const innerState = fromJS({
'test-grid': {
Expand Down

0 comments on commit 01830cf

Please sign in to comment.