| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import Connection from "~/services/connection/connection";
- import axios from "~/plugins/Data/axios";
- import {HTTP_METHOD, QUERY_TYPE} from "~/types/enums";
- import {NuxtAxiosInstance} from "@nuxtjs/axios";
- let $connection:any
- const axiosMock = axios as jest.Mocked<NuxtAxiosInstance>;
- beforeAll(()=>{
- $connection = new Connection()
- Connection.initConnector(axiosMock)
- })
- describe('invoke()', () => {
- it('should throw an error if the method is unknown', ()=>{
- expect( () => $connection.invoke('GETS', 'users', {type: QUERY_TYPE.MODEL})).toThrow()
- })
- 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();
- })
- })
- })
|