connection.spec.ts 3.2 KB

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