contact.js 977 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const { contacts } = require("./data");
  2. const getList = jest.fn(
  3. (
  4. _userId,
  5. { _sortBy, _sortByDesc, _filter, limit = "5", page = "1", _sub }
  6. ) => {
  7. return { contacts, total: contacts.length, limit, page };
  8. }
  9. );
  10. const getById = jest.fn((id, _userId) => {
  11. const [contact] = contacts.filter((el) => el._id === id);
  12. return contact;
  13. });
  14. const add = jest.fn((newContact) => {
  15. const contact = { ...newContact, _id: "604cec3345d8c632bc1fake3" };
  16. contacts.push(contact);
  17. return contact;
  18. });
  19. const update = jest.fn((id, body, _userId) => {
  20. let [contact] = contacts.filter((el) => el._id === id);
  21. if (contact) return (contact = { ...contact, ...body });
  22. });
  23. const remove = jest.fn((id, _userId) => {
  24. const index = contacts.findIndex((el) => String(el._id) === String(id));
  25. if (index === -1) return null;
  26. const [contact] = contacts.splice(index, 1);
  27. return contact;
  28. });
  29. module.exports = {
  30. getList,
  31. getById,
  32. add,
  33. remove,
  34. update,
  35. };