import { NuxtAxiosInstance } from '@nuxtjs/axios' import Connection from '~/services/connection/connection' import axios from '~/plugins/Data/axios' import { HTTP_METHOD, QUERY_TYPE } from '~/types/enums' const axiosMock = axios as jest.Mocked beforeAll(() => { Connection.initConnector(axiosMock) }) describe('invoke()', () => { describe('getItem()', () => { it('should return item data', async () => { Connection.connector.$request = jest.fn().mockReturnValue({ data: 'data user 1' }) const response = await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL }) expect(response).toStrictEqual({ data: 'data user 1' }) }) it('should call getItem', async () => { Connection.getItem = jest.fn().mockReturnValue({}) await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL, id: 1 }) expect(Connection.getItem).toHaveBeenCalled() }) }) describe('getCollection()', () => { it('should return collection data', async () => { Connection.connector.$request = jest.fn().mockReturnValue([{ data: 'data user 1' }, { data: 'data user 2' }]) const response = await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL }) expect(response).toStrictEqual([{ data: 'data user 1' }, { data: 'data user 2' }]) }) it('should call getCollection and return collection data', async () => { Connection.getCollection = jest.fn().mockReturnValue({}) await Connection.invoke(HTTP_METHOD.GET, 'users', { type: QUERY_TYPE.MODEL }) expect(Connection.getCollection).toHaveBeenCalled() }) }) describe('put()', () => { it('should throw an error if data missing', async () => { expect(() => Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL })).toThrow() }) it('should return item data', async () => { Connection.connector.$request = jest.fn().mockReturnValue({ data: 'data user 1' }) const response = await Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL, id: 1, data: {} }) expect(response).toStrictEqual({ data: 'data user 1' }) }) it('should call put and return item data', async () => { Connection.put = jest.fn().mockReturnValue({}) await Connection.invoke(HTTP_METHOD.PUT, 'users', { type: QUERY_TYPE.MODEL, id: 1, data: {} }) expect(Connection.put).toHaveBeenCalled() }) }) describe('deleteItem()', () => { it('should delete item', async () => { Connection.connector.$request = jest.fn().mockReturnValue({}) const response = await Connection.invoke(HTTP_METHOD.DELETE, 'users', { type: QUERY_TYPE.MODEL, id: 1 }) expect(response).toStrictEqual({}) }) it('should call deleteItem', async () => { Connection.deleteItem = jest.fn().mockReturnValue({}) await Connection.invoke(HTTP_METHOD.DELETE, 'users', { type: QUERY_TYPE.MODEL, id: 1 }) expect(Connection.deleteItem).toHaveBeenCalled() }) }) })