| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- 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'
- import { DataPersisterArgs } from '~/types/interfaces'
- const axiosMock = axios as jest.Mocked<NuxtAxiosInstance>
- 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: {} } as DataPersisterArgs)
- 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: {} } as DataPersisterArgs)
- 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()
- })
- })
- })
|