adding a reducer + test

This commit is contained in:
ronronscelestes 2021-08-17 10:39:42 +02:00
parent c270560007
commit 834e0e9855
3 changed files with 147 additions and 98 deletions

View File

@ -200,26 +200,32 @@ function ListView() {
} }
}; };
const handleSelectAllCheckbox = () => { // const handleSelectAllCheckbox = () => {
const webhooksIds = webhooks.map(webhook => webhook.id); // const webhooksIds = webhooks.map(webhook => webhook.id);
if (webhooksToDelete.length === 0) { // if (webhooksToDelete.length === 0) {
webhooksIds.forEach(webhookId => { // webhooksIds.forEach(webhookId => {
// dispatch({
// type: 'SET_WEBHOOKS_TO_DELETE',
// value: true,
// id: webhookId,
// });
// });
// } else {
// webhooksIds.forEach(webhookId => {
// dispatch({
// type: 'SET_WEBHOOKS_TO_DELETE',
// value: false,
// id: webhookId,
// });
// });
// }
// };
const handleSelectAllCheckbox = () => {
dispatch({ dispatch({
type: 'SET_WEBHOOKS_TO_DELETE', type: 'SET_ALL_WEBHOOKS_TO_DELETE',
value: true,
id: webhookId,
}); });
});
} else {
webhooksIds.forEach(webhookId => {
dispatch({
type: 'SET_WEBHOOKS_TO_DELETE',
value: false,
id: webhookId,
});
});
}
}; };
const handleSelectOneCheckbox = (value, id) => { const handleSelectOneCheckbox = (value, id) => {

View File

@ -34,6 +34,15 @@ const reducer = (state, action) =>
break; break;
} }
case 'SET_ALL_WEBHOOKS_TO_DELETE': {
if (state.webhooksToDelete.length === 0) {
draftState.webhooksToDelete = state.webhooks.map(webhook => webhook.id);
} else {
draftState.webhooksToDelete = [];
}
break;
}
case 'WEBHOOKS_DELETED': { case 'WEBHOOKS_DELETED': {
draftState.webhooks = state.webhooks.filter( draftState.webhooks = state.webhooks.filter(
webhook => !state.webhooksToDelete.includes(webhook.id) webhook => !state.webhooksToDelete.includes(webhook.id)

View File

@ -7,92 +7,92 @@ describe('Admin | containers | Webhooks | ListView | reducer', () => {
webhookToDelete: null, webhookToDelete: null,
}; };
describe('Load webhooks', () => { // describe('Load webhooks', () => {
it('should update webhooks with received data', () => { // it('should update webhooks with received data', () => {
const state = initialState; // const state = initialState;
const receivedData = [ // const receivedData = [
{ // {
id: 1, // id: 1,
name: 'webhook 1', // name: 'webhook 1',
url: 'http://localhost:5000', // url: 'http://localhost:5000',
headers: {}, // headers: {},
events: ['entry.create', 'entry.update', 'entry.delete'], // events: ['entry.create', 'entry.update', 'entry.delete'],
isEnabled: true, // isEnabled: true,
}, // },
{ // {
id: 2, // id: 2,
name: 'webhook 2', // name: 'webhook 2',
url: 'http://localhost:4000', // url: 'http://localhost:4000',
headers: {}, // headers: {},
events: ['media.create', 'media.update'], // events: ['media.create', 'media.update'],
isEnabled: false, // isEnabled: false,
}, // },
]; // ];
const action = { // const action = {
type: 'GET_DATA_SUCCEEDED', // type: 'GET_DATA_SUCCEEDED',
data: receivedData, // data: receivedData,
}; // };
const expectedState = { ...state, webhooks: receivedData }; // const expectedState = { ...state, webhooks: receivedData };
expect(reducer(state, action)).toEqual(expectedState); // expect(reducer(state, action)).toEqual(expectedState);
}); // });
}); // });
describe('Update webhook', () => { // describe('Update webhook', () => {
it('should toggle isEnabled parameter', () => { // it('should toggle isEnabled parameter', () => {
const webhooks = [ // const webhooks = [
{ // {
id: 1, // id: 1,
name: 'webhook 1', // name: 'webhook 1',
url: 'http://localhost:5000', // url: 'http://localhost:5000',
headers: {}, // headers: {},
events: ['entry.create', 'entry.update', 'entry.delete'], // events: ['entry.create', 'entry.update', 'entry.delete'],
isEnabled: true, // isEnabled: true,
}, // },
{ // {
id: 2, // id: 2,
name: 'webhook 2', // name: 'webhook 2',
url: 'http://localhost:4000', // url: 'http://localhost:4000',
headers: {}, // headers: {},
events: ['media.create', 'media.update'], // events: ['media.create', 'media.update'],
isEnabled: false, // isEnabled: false,
}, // },
]; // ];
const state = { ...initialState, webhooks }; // const state = { ...initialState, webhooks };
const action = { // const action = {
type: 'SET_WEBHOOK_ENABLED', // type: 'SET_WEBHOOK_ENABLED',
keys: [1, 'isEnabled'], // keys: [1, 'isEnabled'],
value: true, // value: true,
}; // };
const expectedState = { // const expectedState = {
...state, // ...state,
webhooks: [ // webhooks: [
{ // {
id: 1, // id: 1,
name: 'webhook 1', // name: 'webhook 1',
url: 'http://localhost:5000', // url: 'http://localhost:5000',
headers: {}, // headers: {},
events: ['entry.create', 'entry.update', 'entry.delete'], // events: ['entry.create', 'entry.update', 'entry.delete'],
isEnabled: true, // isEnabled: true,
}, // },
{ // {
id: 2, // id: 2,
name: 'webhook 2', // name: 'webhook 2',
url: 'http://localhost:4000', // url: 'http://localhost:4000',
headers: {}, // headers: {},
events: ['media.create', 'media.update'], // events: ['media.create', 'media.update'],
isEnabled: true, // isEnabled: true,
}, // },
], // ],
}; // };
expect(reducer(state, action)).toEqual(expectedState); // expect(reducer(state, action)).toEqual(expectedState);
}); // });
}); // });
describe('Delete webhooks', () => { describe('Delete webhooks', () => {
it('should set a webhook id to webhookToDelete', () => { it('should set a webhook id to webhookToDelete', () => {
@ -288,4 +288,38 @@ describe('Admin | containers | Webhooks | ListView | reducer', () => {
expect(reducer(state, action)).toEqual(expectedState); expect(reducer(state, action)).toEqual(expectedState);
}); });
}); });
it('should clear webhooksToDelete when webhooksToDelete length > 0', () => {
const webhooks = [
{
id: 3,
name: 'webhook 1',
url: 'http://localhost:5000',
headers: {},
events: ['entry.create', 'entry.update', 'entry.delete'],
isEnabled: true,
},
{
id: 4,
name: 'webhook 2',
url: 'http://localhost:4000',
headers: {},
events: ['media.create', 'media.update'],
isEnabled: false,
},
{
id: 5,
name: 'webhook 2',
url: 'http://localhost:4000',
headers: {},
events: ['media.create', 'media.update'],
isEnabled: false,
},
];
const state = { ...initialState, webhooks, webhooksToDelete: [3] };
console.log(state);
});
it('should add all webhooks in webhooksToDelete when webhooksToDelete length === 0', () => {});
}); });