connection.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. const mockFn = jest.fn();
  8. beforeAll(() => {
  9. Connection.initConnector(axiosMock)
  10. })
  11. describe('invoke()', () => {
  12. describe('getItem()', () => {
  13. it('should return item data', async () => {
  14. Connection.connector.$request = mockFn.mockReturnValue({ data: 'data user 1' })
  15. const response = await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL })
  16. expect(response).toStrictEqual({ data: 'data user 1' })
  17. })
  18. it('should call getItem', async () => {
  19. Connection.getItem = mockFn.mockReturnValue({})
  20. await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL, id: 1 })
  21. expect(Connection.getItem).toHaveBeenCalled()
  22. })
  23. })
  24. describe('getCollection() for Image type', () => {
  25. it('should call getCollection with a specific config', async () => {
  26. Connection.request = mockFn.mockReturnValue({})
  27. await Connection.invoke(HTTP_METHOD.GET, 'files/1/download', { showProgress: false, type: QUERY_TYPE.IMAGE })
  28. expect(Connection.request).toHaveBeenCalled()
  29. expect(Connection.request).toBeCalledWith({
  30. 'method': HTTP_METHOD.GET,
  31. 'progress': false,
  32. 'responseType': 'blob',
  33. 'url': 'files/1/download',
  34. 'params': {}
  35. }
  36. )
  37. })
  38. })
  39. describe('getCollection()', () => {
  40. it('should return collection data', async () => {
  41. Connection.connector.$request = mockFn.mockReturnValue([{ data: 'data user 1' }, { data: 'data user 2' }])
  42. const response = await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL })
  43. expect(response).toStrictEqual([{ data: 'data user 1' }, { data: 'data user 2' }])
  44. })
  45. it('should call getCollection and return collection data', async () => {
  46. Connection.getCollection = mockFn.mockReturnValue({})
  47. await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL })
  48. expect(Connection.getCollection).toHaveBeenCalled()
  49. })
  50. })
  51. describe('put()', () => {
  52. it('should throw an error if data missing', async () => {
  53. expect(() => Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL })).toThrow()
  54. })
  55. it('should return item data', async () => {
  56. Connection.connector.$request = mockFn.mockReturnValue({ data: 'data user 1' })
  57. const response = await Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL, id: 1, data: {} } as DataPersisterArgs)
  58. expect(response).toStrictEqual({ data: 'data user 1' })
  59. })
  60. it('should call put and return item data', async () => {
  61. Connection.put = mockFn.mockReturnValue({})
  62. await Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL, id: 1, data: {} } as DataPersisterArgs)
  63. expect(Connection.put).toHaveBeenCalled()
  64. })
  65. })
  66. describe('deleteItem()', () => {
  67. it('should delete item', async () => {
  68. Connection.connector.$request = mockFn.mockReturnValue({})
  69. const response = await Connection.invoke(HTTP_METHOD.DELETE, 'users', { type: QUERY_TYPE.MODEL, id: 1 })
  70. expect(response).toStrictEqual({})
  71. })
  72. it('should call deleteItem', async () => {
  73. Connection.deleteItem = mockFn.mockReturnValue({})
  74. await Connection.invoke(HTTP_METHOD.DELETE, 'users', { type: QUERY_TYPE.MODEL, id: 1 })
  75. expect(Connection.deleteItem).toHaveBeenCalled()
  76. })
  77. })
  78. })