connection.spec.ts 3.1 KB

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