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 webhooksIds = webhooks.map(webhook => webhook.id);
// const handleSelectAllCheckbox = () => {
// const webhooksIds = webhooks.map(webhook => webhook.id);
if (webhooksToDelete.length === 0) {
webhooksIds.forEach(webhookId => {
// if (webhooksToDelete.length === 0) {
// 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({
type: 'SET_WEBHOOKS_TO_DELETE',
value: true,
id: webhookId,
type: 'SET_ALL_WEBHOOKS_TO_DELETE',
});
});
} else {
webhooksIds.forEach(webhookId => {
dispatch({
type: 'SET_WEBHOOKS_TO_DELETE',
value: false,
id: webhookId,
});
});
}
};
const handleSelectOneCheckbox = (value, id) => {

View File

@ -34,6 +34,15 @@ const reducer = (state, action) =>
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': {
draftState.webhooks = state.webhooks.filter(
webhook => !state.webhooksToDelete.includes(webhook.id)

View File

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