connection.spec.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }
  35. )
  36. })
  37. })
  38. describe('getCollection()', () => {
  39. it('should return collection data', async () => {
  40. Connection.connector.$request = mockFn.mockReturnValue([{ data: 'data user 1' }, { data: 'data user 2' }])
  41. const response = await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL })
  42. expect(response).toStrictEqual([{ data: 'data user 1' }, { data: 'data user 2' }])
  43. })
  44. it('should call getCollection and return collection data', async () => {
  45. Connection.getCollection = mockFn.mockReturnValue({})
  46. await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL })
  47. expect(Connection.getCollection).toHaveBeenCalled()
  48. })
  49. })
  50. describe('put()', () => {
  51. it('should throw an error if data missing', async () => {
  52. expect(() => Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL })).toThrow()
  53. })
  54. it('should return item data', async () => {
  55. Connection.connector.$request = mockFn.mockReturnValue({ data: 'data user 1' })
  56. const response = await Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL, id: 1, data: {} } as DataPersisterArgs)
  57. expect(response).toStrictEqual({ data: 'data user 1' })
  58. })
  59. it('should call put and return item data', async () => {
  60. Connection.put = mockFn.mockReturnValue({})
  61. await Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL, id: 1, data: {} } as DataPersisterArgs)
  62. expect(Connection.put).toHaveBeenCalled()
  63. })
  64. })
  65. describe('deleteItem()', () => {
  66. it('should delete item', async () => {
  67. Connection.connector.$request = mockFn.mockReturnValue({})
  68. const response = await Connection.invoke(HTTP_METHOD.DELETE, 'users', { type: QUERY_TYPE.MODEL, id: 1 })
  69. expect(response).toStrictEqual({})
  70. })
  71. it('should call deleteItem', async () => {
  72. Connection.deleteItem = mockFn.mockReturnValue({})
  73. await Connection.invoke(HTTP_METHOD.DELETE, 'users', { type: QUERY_TYPE.MODEL, id: 1 })
  74. expect(Connection.deleteItem).toHaveBeenCalled()
  75. })
  76. })
  77. })