connection.spec.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { NuxtAxiosInstance } from '@nuxtjs/axios'
  2. import Connection from '~/services/connection/connection'
  3. import axios from '~/plugins/Data/axios'
  4. import { HTTP_METHOD, QUERY_TYPE } from '~/types/enums'
  5. const axiosMock = axios as jest.Mocked<NuxtAxiosInstance>
  6. beforeAll(() => {
  7. Connection.initConnector(axiosMock)
  8. })
  9. describe('invoke()', () => {
  10. describe('getItem()', () => {
  11. it('should return item data', async () => {
  12. Connection.connector.$request = jest.fn().mockReturnValue({ data: 'data user 1' })
  13. const response = await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL })
  14. expect(response).toStrictEqual({ data: 'data user 1' })
  15. })
  16. it('should call getItem', async () => {
  17. Connection.getItem = jest.fn().mockReturnValue({})
  18. await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL, id: 1 })
  19. expect(Connection.getItem).toHaveBeenCalled()
  20. })
  21. })
  22. describe('getCollection()', () => {
  23. it('should return collection data', async () => {
  24. Connection.connector.$request = jest.fn().mockReturnValue([{ data: 'data user 1' }, { data: 'data user 2' }])
  25. const response = await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL })
  26. expect(response).toStrictEqual([{ data: 'data user 1' }, { data: 'data user 2' }])
  27. })
  28. it('should call getCollection and return collection data', async () => {
  29. Connection.getCollection = jest.fn().mockReturnValue({})
  30. await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL })
  31. expect(Connection.getCollection).toHaveBeenCalled()
  32. })
  33. })
  34. describe('put()', () => {
  35. it('should throw an error if data missing', async () => {
  36. expect(() => Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL })).toThrow()
  37. })
  38. it('should return item data', async () => {
  39. Connection.connector.$request = jest.fn().mockReturnValue({ data: 'data user 1' })
  40. const response = await Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL, id: 1, data: {} })
  41. expect(response).toStrictEqual({ data: 'data user 1' })
  42. })
  43. it('should call put and return item data', async () => {
  44. Connection.put = jest.fn().mockReturnValue({})
  45. await Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL, id: 1, data: {} })
  46. expect(Connection.put).toHaveBeenCalled()
  47. })
  48. })
  49. describe('deleteItem()', () => {
  50. it('should delete item', async () => {
  51. Connection.connector.$request = jest.fn().mockReturnValue({})
  52. const response = await Connection.invoke(HTTP_METHOD.DELETE, 'users', { type: QUERY_TYPE.MODEL, id: 1 })
  53. expect(response).toStrictEqual({})
  54. })
  55. it('should call deleteItem', async () => {
  56. Connection.deleteItem = jest.fn().mockReturnValue({})
  57. await Connection.invoke(HTTP_METHOD.DELETE, 'users', { type: QUERY_TYPE.MODEL, id: 1 })
  58. expect(Connection.deleteItem).toHaveBeenCalled()
  59. })
  60. })
  61. })