connection.spec.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import Connection from "~/services/connection/connection";
  2. import axios from "~/plugins/Data/axios";
  3. import {HTTP_METHOD, QUERY_TYPE} from "~/types/enums";
  4. import {NuxtAxiosInstance} from "@nuxtjs/axios";
  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. })