Initial commit

This commit is contained in:
2024-03-05 11:36:21 +03:00
commit bf2f060b94
212 changed files with 100448 additions and 0 deletions

View File

@@ -0,0 +1,204 @@
import { expect, test } from 'vitest'
import { convertObject, convertList } from '@helpers/server-routes/adapter/adapter.js'
test('parse object from api', () => {
const targetObject = {
"id": 1,
"created_at": "2024-02-15T17:24:52.755254148+03:00",
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
"deleted_at": null,
"name": "jsonplaceholder.typicode.com",
"port": 9965,
"proxy_ip": "172.25.78.153",
"site_ip": "172.25.78.36",
"internet_uri": "localhost",
"description": "localhost",
"is_online": true
}
const expectedObject = {
id: 1,
name2: "jsonplaceholder.typicode.com",
port2: 9965,
proxy_ip: "172.25.78.153",
site_ip: "172.25.78.36",
internet_uri: "localhost",
description: "localhost",
is_online: true,
}
const config = {
id: "id",
name: "name2",
port: "port2",
proxy_ip: "proxy_ip",
site_ip: "site_ip",
internet_uri: "internet_uri",
description: "description",
is_online: "is_online",
}
expect(convertObject(targetObject, {config})).toEqual(expectedObject)
})
test('adapt list objects without callback', () => {
const targetObject1 = {
"id": 1,
"created_at": "2024-02-15T17:24:52.755254148+03:00",
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
"deleted_at": null,
"name": "jsonplaceholder.typicode.com",
"port": 9965,
"proxy_ip": "172.25.78.153",
"site_ip": "172.25.78.36",
"internet_uri": "localhost",
"description": "localhost",
"is_online": true
}
const targetObject2 = {
"id": 1,
"created_at": "2024-02-15T17:24:52.755254148+03:00",
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
"deleted_at": null,
"name": "jsonplaceholder.typicode.com",
"port": 9965,
"proxy_ip": "172.25.78.153",
"site_ip": "172.25.78.36",
"internet_uri": "localhost",
"description": "localhost",
"is_online": true
}
const targetObject3 = {
"id": 1,
"created_at": "2024-02-15T17:24:52.755254148+03:00",
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
"deleted_at": null,
"name": "jsonplaceholder.typicode.com",
"port": 9965,
"proxy_ip": "172.25.78.153",
"site_ip": "172.25.78.36",
"internet_uri": "localhost",
"description": "localhost",
"is_online": true
}
const targetList = [targetObject1, targetObject2, targetObject3]
const selfObject1 = {
id: 1,
name2: "jsonplaceholder.typicode.com",
port2: 9965,
proxy_ip: "172.25.78.153",
site_ip: "172.25.78.36",
internet_uri: "localhost",
description: "localhost",
is_online: true,
}
const selfObject2 = {
id: 1,
name2: "jsonplaceholder.typicode.com",
port2: 9965,
proxy_ip: "172.25.78.153",
site_ip: "172.25.78.36",
internet_uri: "localhost",
description: "localhost",
is_online: true,
}
const selfObject3 = {
id: 1,
name2: "jsonplaceholder.typicode.com",
port2: 9965,
proxy_ip: "172.25.78.153",
site_ip: "172.25.78.36",
internet_uri: "localhost",
description: "localhost",
is_online: true,
}
const selfList = [selfObject1, selfObject2, selfObject3]
const config = {
id: "id",
name: "name2",
port: "port2",
proxy_ip: "proxy_ip",
site_ip: "site_ip",
internet_uri: "internet_uri",
description: "description",
is_online: "is_online",
}
expect(convertList(targetList, {config})).toEqual(selfList)
})
test('adapt list objects with callback', () => {
const targetObject1 = {
"id": 1,
"created_at": "2024-02-15T17:24:52.755254148+03:00",
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
"deleted_at": null,
"name": "jsonplaceholder.typicode.com",
"port": 9965,
"proxy_ip": "172.25.78.153",
"site_ip": "172.25.78.36",
"internet_uri": "localhost",
"description": "localhost",
"is_online": true
}
const targetObject2 = {
"id": 1,
"created_at": "2024-02-15T17:24:52.755254148+03:00",
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
"deleted_at": null,
"name": "jsonplaceholder.typicode.com",
"port": 9965,
"proxy_ip": "172.25.78.153",
"site_ip": "172.25.78.36",
"internet_uri": "localhost",
"description": "localhost",
"is_online": false
}
const targetList = [targetObject1, targetObject2]
const expectedObject1 = {
id: 1,
name2: "jsonplaceholder.typicode.com",
port2: 9965,
proxy_ip: "172.25.78.153",
site_ip: "172.25.78.36",
internet_uri: "localhost",
description: "localhost",
state: "active",
}
const expectedObject2 = {
id: 1,
name2: "jsonplaceholder.typicode.com",
port2: 9965,
proxy_ip: "172.25.78.153",
site_ip: "172.25.78.36",
internet_uri: "localhost",
description: "localhost",
state: "disabled",
}
const expectedList = [expectedObject1, expectedObject2]
const config = {
id: "id",
name: "name2",
port: "port2",
proxy_ip: "proxy_ip",
site_ip: "site_ip",
internet_uri: "internet_uri",
description: "description",
}
const callback = (el, {is_online}) => ({...el, state: is_online ? "active": "disabled"})
expect(convertList(targetList, {config, callback})).toEqual(expectedList)
})

View File

@@ -0,0 +1,310 @@
import {describe, expect, beforeEach} from 'vitest'
import {createStore} from 'vuex'
import {store} from '@store/modules/proxy/index.js'
import {addedNewRoute, updatedRoute, sortedRoutes, deletedRoute} from '@store/modules/proxy/helpers.js'
const selectedSite = store.state.selectedSite
const routes = store.state.routes
const routesState = store.state.routesState
const setSelectedSite = store.mutations.setSelectedSite
const setRoutes = store.mutations.setRoutes
const setRoutesState = store.mutations.setRoutesState
const addNewRouteLayout = store.actions.addNewRouteLayout
const uploadSiteRoutes = store.actions.uploadSiteRoutes
const updateRouteRow = store.actions.updateRouteRow
const updateRoutesWithApi = store.actions.updateRoutesWithApi
const removeRoute = store.actions.removeRoute
const resetStore = store.actions.resetStore
const defaultRoutes = [{
"id": 7,
"created_at": "2024-02-27T12:02:34.666042252+03:00",
"updated_at": "2024-02-27T12:02:34.666042252+03:00",
"deleted_at": null,
"server_id": -1,
"path": "/route",
"role": 0,
"description": "with route",
"order": 0,
"deepness": 0,
"is_cb_on": true,
"cb_request_limit": 0,
"cb_min_requests": 7,
"cb_error_threshold_percentage": 0,
"cb_interval_duration": 0,
"cb_open_state_timeout": 0,
"is_online": false
},
{
"id": 8,
"created_at": "2024-02-27T12:05:45.268921795+03:00",
"updated_at": "2024-02-27T12:05:45.268921795+03:00",
"deleted_at": null,
"server_id": 7,
"path": "new/3",
"role": 0,
"description": "",
"order": 0,
"deepness": 0,
"is_cb_on": false,
"cb_request_limit": 3,
"cb_min_requests": 5,
"cb_error_threshold_percentage": 0,
"cb_interval_duration": 0,
"cb_open_state_timeout": 0,
"is_online": true
},
{
"id": 9,
"created_at": "2024-02-27T12:11:09.940033992+03:00",
"updated_at": "2024-02-27T12:11:09.940033992+03:00",
"deleted_at": null,
"server_id": -1,
"path": "new/1",
"role": 0,
"description": "",
"order": 0,
"deepness": 9,
"is_cb_on": true,
"cb_request_limit": 0,
"cb_min_requests": 0,
"cb_error_threshold_percentage": 0,
"cb_interval_duration": 0,
"cb_open_state_timeout": 5,
"is_online": false
}]
const mockStore = createStore({
state: {
selectedSite: selectedSite,
routes: routes,
routesState: routesState,
},
getters: {
selectedSite: (state) => state.selectedSite,
},
mutations: {
setSelectedSite: setSelectedSite,
setRoutes: setRoutes,
setRoutesState: setRoutesState
},
actions: {
addNewRouteLayout: addNewRouteLayout,
uploadSiteRoutes: uploadSiteRoutes,
updateRouteRow: updateRouteRow,
updateRoutesWithApi: updateRoutesWithApi,
removeRoute: removeRoute,
resetStore
},
})
describe('actions', () => {
//eslint-disable-next-line no-undef
it('action addNewRouteLayout - added fields for new Route', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
mockStore.commit('setSelectedSite', {id: 9}) // added id for selected site, need to this action for bind server_id
mockStore.dispatch('addNewRouteLayout')
expect(mockStore.state.routes[0]).toMatchObject({action: "create", server_id: 9})
})
//eslint-disable-next-line no-undef
it('action updateRouteRow - edited fields values, in selected row', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
const editedRoute = {
"id": 8,
"created_at": "2024-02-27T12:05:45.268921795+03:00",
"updated_at": "2024-02-27T12:05:45.268921795+03:00",
"deleted_at": null,
"server_id": 7,
"path": "new/3",
"role": 0,
"description": "Testing edited route",
"order": 0,
"deepness": 0,
"is_cb_on": true,
"cb_request_limit": 3,
"cb_min_requests": 5,
"cb_error_threshold_percentage": 0,
"cb_interval_duration": 7,
"cb_open_state_timeout": 0,
"is_online": true
}
mockStore.commit('setRoutes', defaultRoutes)
const currentRoute = mockStore.state.routes.find(route => route.id === 8)
expect(currentRoute).toMatchObject({"description": "", "is_cb_on": false, "cb_interval_duration": 0}) // check default values fields in selected route
mockStore.dispatch('updateRouteRow', editedRoute)
const updatedRoute = mockStore.state.routes.find(route => route.id === 8)
expect(updatedRoute).toMatchObject({"description": "Testing edited route", "is_cb_on": true, "cb_interval_duration": 7})
})
//eslint-disable-next-line no-undef
it('action updateRouteRow - edited fields, empty values, in selected row', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
const editedRoute = {
"id": 8,
"created_at": "2024-02-27T12:05:45.268921795+03:00",
"updated_at": "2024-02-27T12:05:45.268921795+03:00",
"deleted_at": null,
"server_id": 7,
"path": "",
"role": 0,
"description": null,
"order": 0,
"deepness": 0,
"is_cb_on": true,
"cb_request_limit": 0,
"cb_min_requests": 5,
"cb_error_threshold_percentage": 0,
"cb_interval_duration": 7,
"cb_open_state_timeout": 0,
"is_online": true
}
mockStore.commit('setRoutes', defaultRoutes)
const currentRoute = mockStore.state.routes.find(route => route.id === 8)
expect(currentRoute).toMatchObject({"cb_request_limit": 3, "path": "new/3", "description": ""}) // check default values fields in selected route
mockStore.dispatch('updateRouteRow', editedRoute)
const updatedRoute = mockStore.state.routes.find(route => route.id === 8)
expect(updatedRoute).toMatchObject({"cb_request_limit": 0, "path": "", "description": null})
})
})
describe('helpers', () => {
//eslint-disable-next-line no-undef
it('added new route to site', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
const editedSite = {
"id": 3,
"created_at": "2024-02-26T17:50:07.191225008+03:00",
"updated_at": "2024-02-28T09:41:49.274089436+03:00",
"deleted_at": null,
"name": "new",
"port": 3645,
"proxy_ip": "172.25.78.151",
"site_ip": "172.25.78.151",
"internet_uri": "",
"description": "new updated...",
"is_online": false
}
const newRoute = {
'action': "create",
"path": "",
"role": 0,
"description": "9",
"order": 0,
"deepness": 7,
"is_cb_on": false,
"cb_request_limit": 0,
"cb_min_requests": 0,
"cb_error_threshold_percentage": 0,
"cb_interval_duration": 3,
"cb_open_state_timeout": 0,
"is_online": true
}
mockStore.commit('setRoutes', [newRoute, ...defaultRoutes])
const updatedRoutes = addedNewRoute(editedSite, mockStore.state.routes)
mockStore.commit('setRoutes', updatedRoutes)
const addedNewRouteToStore = mockStore.state.routes.find(route => route.action === 'create')
expect(addedNewRouteToStore).not.toBe(undefined)
expect(editedSite.id).toEqual(addedNewRouteToStore.server_id) // if true then added new route successfully
})
//eslint-disable-next-line no-undef
it('updated route', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
const editedRoute = {
"id": 8,
"created_at": "2024-02-27T12:05:45.268921795+03:00",
"updated_at": "2024-02-27T12:05:45.268921795+03:00",
"deleted_at": null,
"server_id": 7,
"path": "new/3",
"role": 0,
"description": "",
"order": 0,
"deepness": 9,
"is_cb_on": true,
"cb_request_limit": 3,
"cb_min_requests": 5,
"cb_error_threshold_percentage": 0,
"cb_interval_duration": 7,
"cb_open_state_timeout": 0,
"is_online": true
}
mockStore.commit('setRoutes', defaultRoutes)
const currentRouteBeforeEdit = mockStore.state.routes.find(route => route.id === 8)
expect(currentRouteBeforeEdit).toMatchObject({"deepness": 0, is_cb_on: false, "cb_interval_duration": 0}) // default params route
const updatedRoutes = updatedRoute(editedRoute, mockStore.state.routes)
mockStore.commit('setRoutes', updatedRoutes)
const updatedRouteToStore = mockStore.state.routes.find(route => route.id === 8)
expect(updatedRouteToStore).not.toBe(undefined)
expect(updatedRouteToStore).toMatchObject({"deepness": 9, is_cb_on: true, "cb_interval_duration": 7}) // check new params updated route
})
//eslint-disable-next-line no-undef
it('deleted route', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
const deleteRouteId = 9
mockStore.commit('setRoutes', defaultRoutes)
const currentRouteBeforeDelete = mockStore.state.routes.find(route => route.id === 9)
expect(currentRouteBeforeDelete).toMatchObject({id: 9}) // exists route before deleteting
const updatedRoutes = deletedRoute(deleteRouteId, mockStore.state.routes)
mockStore.commit('setRoutes', updatedRoutes)
const deletedRouteToStore = mockStore.state.routes.find(route => route.id === 9)
expect(deletedRouteToStore).toMatchObject({action: 'remove'}) // to deleted route to be added prop action with value 'remove'
})
//eslint-disable-next-line no-undef
it('sorted routes - sort in descending order, sorted prop: updated_at', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
const newRoute = {
"id": 10,
"created_at": "2024-02-27T12:12:53.295785444+03:00",
"updated_at": "2024-02-27T12:12:53.295785444+03:00",
"deleted_at": null,
"server_id": -1,
"path": "new/12",
"role": 0,
"description": "",
"order": 1,
"deepness": 0,
"is_cb_on": false,
"cb_request_limit": 3,
"cb_min_requests": 5,
"cb_error_threshold_percentage": 0,
"cb_interval_duration": 0,
"cb_open_state_timeout": 0,
"is_online": true
}
mockStore.commit('setRoutes', [...defaultRoutes, newRoute])
const newRouteIdxBeforeSorting = mockStore.state.routes.findIndex(route => route.id === 10)
expect(newRouteIdxBeforeSorting).toBe(3) // default last index this new route in test
const updatedRoutes = sortedRoutes(mockStore.state.routes)
mockStore.commit('setRoutes', updatedRoutes)
const newRouteIdxAfterSorting = mockStore.state.routes.findIndex(route => route.id === 10)
expect(newRouteIdxAfterSorting).toBe(0) // check route with id 10 moved to first index after sorting, because new route have updated_at === created_at for nre routes
})
})

View File

@@ -0,0 +1,345 @@
import {describe, expect, beforeEach} from 'vitest'
import {createStore} from 'vuex'
import {store} from '@store/modules/proxy/index.js'
import {addedSite, updatedSite, removedNewSite, deletedSite} from '@store/modules/proxy/helpers.js'
const sites = store.state.sites
const selectedSite = store.state.selectedSite
const isSaveData = store.state.isSaveData
const routes = store.state.routes
const routesState = store.state.routesState
const setSites = store.mutations.setSites
const setSelectedSite = store.mutations.setSelectedSite
const setIsSaveData = store.mutations.setIsSaveData
const setRoutes = store.mutations.setRoutes
const setRoutesState = store.mutations.setRoutesState
const addNewSiteLayout = store.actions.addNewSiteLayout
const createNewSite = store.actions.createNewSite
const breakeAddingSite = store.actions.breakeAddingSite
const editSelectedSite = store.actions.editSelectedSite
const saveSite = store.actions.saveSite
const breakSavingSite = store.actions.breakSavingSite
const removeSite = store.actions.removeSite
const resetStore = store.actions.resetStore
const defaultSites = [ {
"id": 1,
"created_at": "2024-02-22T17:08:37.715772388+03:00",
"updated_at": "2024-02-26T14:11:38.64094899+03:00",
"deleted_at": null,
"name": "jsonplaceholder.typicode.com",
"port": 9965,
"proxy_ip": "172.25.78.153",
"site_ip": "172.25.78.153",
"internet_uri": "localhost",
"description": "localhost",
"is_online": true
},
{
"id": 2,
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "2024-02-27T17:53:14.070484767+03:00",
"deleted_at": null,
"name": "new",
"port": 3645,
"proxy_ip": "172.25.78.151",
"site_ip": "172.25.78.151",
"internet_uri": "",
"description": "create... upd...",
"is_online": true
},
{
"id": 3,
"created_at": "2024-02-26T17:50:07.191225008+03:00",
"updated_at": "2024-02-28T09:41:49.274089436+03:00",
"deleted_at": null,
"name": "new",
"port": 3645,
"proxy_ip": "172.25.78.151",
"site_ip": "172.25.78.151",
"internet_uri": "",
"description": "new updated...",
"is_online": false
}]
const mockStore = createStore({
state: {
sites: sites,
selectedSite: selectedSite,
isSaveData: isSaveData,
routes: routes,
routesState: routesState,
},
getters: {
sites: (state) => state.sites,
selectedSite: (state) => state.selectedSite,
},
mutations: {
setSites: setSites,
setSelectedSite: setSelectedSite,
setIsSaveData: setIsSaveData,
setRoutes: setRoutes,
setRoutesState: setRoutesState
},
actions: {
addNewSiteLayout: addNewSiteLayout,
createNewSite: createNewSite,
breakeAddingSite: breakeAddingSite,
editSelectedSite: editSelectedSite,
saveSite: saveSite,
breakSavingSite: breakSavingSite,
removeSite: removeSite,
resetStore
},
})
describe('mutations', () => {
//eslint-disable-next-line no-undef
it('Set to empty Services', () => {
const state = { sites: sites }
setSites(state, null)
expect(state.sites).to.equal(null)
})
//eslint-disable-next-line no-undef
it('Add new Service', () => {
const newSite = {
"id": 34,
"created_at": "2024-02-28T15:32:21.187767309+03:00",
"updated_at": "2024-02-28T15:32:50.820118413+03:00",
"deleted_at": null,
"name": "Test site 10",
"port": 7575,
"proxy_ip": "172.25.78.151",
"site_ip": "172.25.78.151",
"internet_uri": "",
"description": "10 test create",
"is_online": true
}
const updatedSites = [...sites, newSite]
const state = { sites: sites }
setSites(state, updatedSites)
expect(state.sites[0]).to.toMatchObject({"name": "Test site 10", "port": 7575, "id": 34, "is_online": true})
})
})
describe('actions', () => {
//eslint-disable-next-line no-undef
it('action addNewSiteLayout - added fields for new Service', async () => {
mockStore.dispatch('addNewSiteLayout')
expect(mockStore.state.sites[0]).toMatchObject({id: -1})
})
//eslint-disable-next-line no-undef
it('action editSelectedSite - edited fields values in selected site', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
mockStore.commit('setSelectedSite', {id: 7, name: 'custom test name', port: 3333, description: 'default'}) // added params - key, vakue for selected site
mockStore.dispatch('editSelectedSite', {key: 'name', value: 'edited test name'})
mockStore.dispatch('editSelectedSite', {key: 'port', value: 5555})
mockStore.dispatch('editSelectedSite', {key: 'description', value: 'updated'})
expect(mockStore.state.selectedSite).toMatchObject({name: 'edited test name', port: 5555, description: 'updated'})
})
//eslint-disable-next-line no-undef
it('action editSelectedSite - edited fields empty values in selected site', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
mockStore.commit('setSelectedSite', {id: 7, name: '', port: '', description: ''}) // added params - key, vakue for selected site
mockStore.dispatch('editSelectedSite', {key: 'name', value: ''})
mockStore.dispatch('editSelectedSite', {key: 'port', value: ''})
mockStore.dispatch('editSelectedSite', {key: 'description', value: ''})
expect(mockStore.state.selectedSite).toMatchObject({name: '', port: '', description: ''})
})
//eslint-disable-next-line no-undef
it('action breakSavingSite - cleared selected site, replaced to default status routesState', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
mockStore.commit('setSelectedSite', {id: 9, name: 'custom test name', port: 3333, description: 'default'}) // added params - key, vakue for selected site
mockStore.commit('setRoutesState', 'active') // added status for RoutesState
expect(mockStore.state.routesState).toBe('active') // check RoutesState status
mockStore.dispatch('breakSavingSite', {key: 'name', value: 'edited test name'})
expect(mockStore.state.selectedSite).toBeNull()
expect(mockStore.state.routesState).toBe('await')
})
//eslint-disable-next-line no-undef
it('action resetStore - cleared all values in store', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
expect(mockStore.state.selectedSite).toBeNull() // check default statuses
expect(mockStore.state.routesState).toBe('await') // check default statuses
expect(mockStore.state.isSaveData).toBe(false) // check default statuses
mockStore.commit('setSelectedSite', {id: 9, name: 'custom test name', port: 3333, description: 'default'}) // added params - key, vakue for selected site
mockStore.commit('setRoutesState', 'active') // updated status for RoutesState
mockStore.commit('setIsSaveData', true) // updated status for isSaveData
expect(mockStore.state.selectedSite).toMatchObject({id: 9, name: 'custom test name', port: 3333, description: 'default'}) // check updated statuses
expect(mockStore.state.routesState).toBe('active') // check updated statuses
expect(mockStore.state.isSaveData).toBe(true) // check updated statuses
mockStore.dispatch('resetStore')
expect(mockStore.state.selectedSite).toBeNull() // again check default statuses
expect(mockStore.state.routesState).toBe('await') // again check default statuses
expect(mockStore.state.isSaveData).toBe(false) // again check default statuses
})
})
describe('helpers', () => {
//eslint-disable-next-line no-undef
it('added new site', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
const newSite = {
"id": 34,
"created_at": "2024-02-28T15:32:21.187767309+03:00",
"updated_at": "2024-02-28T15:32:50.820118413+03:00",
"deleted_at": null,
"name": "new site 9",
"port": 7475,
"proxy_ip": "172.25.78.151",
"site_ip": "172.25.78.151",
"internet_uri": "",
"description": "9 create",
"is_online": true
}
mockStore.commit('setSites', defaultSites)
const updatedSites = addedSite(newSite, mockStore.state.sites)
mockStore.commit('setRoutes', updatedSites)
const addedNewSite = mockStore.state.routes.find(route => route.id === 34)
expect(addedNewSite).not.toBe(undefined)
expect(addedNewSite).toMatchObject(newSite)
})
//eslint-disable-next-line no-undef
it('updated site', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
const editedSite = {
"id": 3,
"created_at": "2024-02-26T17:50:07.191225008+03:00",
"updated_at": "2024-02-28T09:41:49.274089436+03:00",
"deleted_at": null,
"name": "edited test name",
"port": 5555,
"proxy_ip": "172.25.78.151",
"site_ip": "172.25.78.151",
"internet_uri": "",
"description": "new updated... updated site",
"is_online": true
}
mockStore.commit('setSites', defaultSites)
const currentSiteBeforeEdit = mockStore.state.sites.find(site => site.id === 3)
expect(currentSiteBeforeEdit).toMatchObject({name: 'new', port: 3645, description: 'new updated...'}) // default params site
const updatedSites = updatedSite(editedSite, mockStore.state.sites)
mockStore.commit('setSites', updatedSites)
const updatedSiteToStore = mockStore.state.sites.find(site => site.id === 3)
expect(updatedSiteToStore).not.toBe(undefined)
expect(updatedSiteToStore).toMatchObject(editedSite)
})
//eslint-disable-next-line no-undef
it('updated site - empty params', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
const editedSite = {
"id": 3,
"created_at": "2024-02-26T17:50:07.191225008+03:00",
"updated_at": "2024-02-28T09:41:49.274089436+03:00",
"deleted_at": null,
"name": "",
"port": null,
"proxy_ip": "172.25.78.151",
"site_ip": "172.25.78.151",
"internet_uri": "",
"description": "",
"is_online": true
}
mockStore.commit('setSites', defaultSites)
const currentSiteBeforeEdit = mockStore.state.sites.find(site => site.id === 3)
expect(currentSiteBeforeEdit).toMatchObject({name: 'new', port: 3645, description: 'new updated...'}) // default params site
const updatedSites = updatedSite(editedSite, mockStore.state.sites)
mockStore.commit('setSites', updatedSites)
const updatedSiteToStore = mockStore.state.sites.find(site => site.id === 3)
expect(updatedSiteToStore).not.toBe(undefined)
expect(updatedSiteToStore).toMatchObject(editedSite)
})
//eslint-disable-next-line no-undef
it('removed new site before saving to server', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
const newSite = {"port": "", "name": "", id: -1}
mockStore.commit('setSites', defaultSites)
const sitesWithNewSite = addedSite(newSite, mockStore.state.sites)
mockStore.commit('setSites', sitesWithNewSite)
const currentSiteBeforeDelete = mockStore.state.sites[0]
expect(currentSiteBeforeDelete).toMatchObject({id: -1}) // default id for new site if this not saving to server
const updatedSites = removedNewSite(mockStore.state.sites)
mockStore.commit('setSites', updatedSites)
const removedSiteToStore = mockStore.state.sites.find(site => site.id === -1)
expect(removedSiteToStore).toBe(undefined) // removed site to be undefined in array sites to store
})
//eslint-disable-next-line no-undef
it('deleted selected site before saving to server', async () => {
beforeEach(() => {
mockStore.dispatch('resetStore')
})
const deleteSiteId = 3
mockStore.commit('setSites', defaultSites)
const currentSiteBeforeDelete = mockStore.state.sites.find(site => site.id === 3)
expect(currentSiteBeforeDelete).toMatchObject({id: 3}) // exists site before deleteting
const updatedSites = deletedSite(deleteSiteId, mockStore.state.sites)
mockStore.commit('setSites', updatedSites)
const deletedSiteToStore = mockStore.state.sites.find(site => site.id === 3)
expect(deletedSiteToStore).toBe(undefined) // deleted site to be undefined in array sites to store
})
})
// describe('class Services', () => {
// it('upload Sites', async () => {
// // const services = new Services(import.meta.env.VITE_API_ADDR, config)
// const equalService = [{
// "id": 1,
// "created_at": "2024-02-22T17:08:37.715772388+03:00",
// "updated_at": "2024-02-26T14:11:38.64094899+03:00",
// "deleted_at": null,
// "name": "jsonplaceholder.typicode.com",
// "port": 9965,
// "proxy_ip": "172.25.78.153",
// "site_ip": "172.25.78.153",
// "internet_uri": "localhost",
// "description": "localhost",
// "is_online": true
// }]
// vi.fn(axios.get).mockResolvedValue({
// data: equalService,
// })
// const services = new Services(import.meta.env.VITE_API_ADDR, config)
// const uploadedServices = await services.getServices()
// mockStore.dispatch('uploadSites')
// expect(uploadedServices[0]).to.toMatchObject(equalService[0])
// })
// })

View File

@@ -0,0 +1,25 @@
import { mount } from '@vue/test-utils'
import { expect, test, describe } from 'vitest'
import PageHeader from "@atoms/PageHeader.vue"
import { createStore } from 'vuex'
import {store as proxy} from '@/store/modules/proxy';
describe("tests PageHeader with vuex", () => {
const store = createStore({
plugins: [],
modules: {
proxy
},
})
test('tests PageHeader with vuex', async () => {
const wrapper = mount(PageHeader, {
global: {
plugins: [store]
}
})
expect(wrapper.text()).toContain('На главную')
})
})

View File

@@ -0,0 +1,177 @@
import {mount} from '@vue/test-utils'
import {expect, it, describe} from 'vitest'
import RoutesList from "@organisms/RoutersEditor/index.vue"
// import RoutersEditor from "@organisms/RoutersEditor/index.vue"
import routeOptions from '@store/modules/proxy/routeOptions.json'
import {createStore} from 'vuex'
const defaultRoutes = [{
"id": 7,
"created_at": "2024-02-27T12:02:34.666042252+03:00",
"updated_at": "2024-02-27T12:02:34.666042252+03:00",
"deleted_at": null,
"server_id": -1,
"path": "/route",
"role": 0,
"description": "with route",
"order": 0,
"deepness": 0,
"is_cb_on": true,
"cb_request_limit": 0,
"cb_min_requests": 7,
"cb_error_threshold_percentage": 0,
"cb_interval_duration": 0,
"cb_open_state_timeout": 0,
"is_online": false
},
{
"id": 8,
"created_at": "2024-02-27T12:05:45.268921795+03:00",
"updated_at": "2024-02-27T12:05:45.268921795+03:00",
"deleted_at": null,
"server_id": 7,
"path": "new/3",
"role": 0,
"description": "",
"order": 0,
"deepness": 0,
"is_cb_on": false,
"cb_request_limit": 3,
"cb_min_requests": 5,
"cb_error_threshold_percentage": 0,
"cb_interval_duration": 0,
"cb_open_state_timeout": 0,
"is_online": true
},
{
"id": 9,
"created_at": "2024-02-27T12:11:09.940033992+03:00",
"updated_at": "2024-02-27T12:11:09.940033992+03:00",
"deleted_at": null,
"server_id": -1,
"path": "new/1",
"role": 0,
"description": "",
"order": 0,
"deepness": 9,
"is_cb_on": true,
"cb_request_limit": 0,
"cb_min_requests": 0,
"cb_error_threshold_percentage": 0,
"cb_interval_duration": 0,
"cb_open_state_timeout": 5,
"is_online": false
}]
const store = createStore({
plugins: [],
modules: {
proxy: {
state: {
routes: [],
routesState: "await",
routesLib: {},
routeOptions: routeOptions
},
getters: {
routes: (state) => state.routes,
routesState: (state) => state.routesState,
routesLib: (state) => state.routesLib,
routeOptions: (state) => state.routeOptions,
},
mutations: {
setRoutes: (state, updval) => state.routes = updval,
setRoutesState: (state, updval) => state.routesState = updval,
setRoutesLib: (state, updval) => state.routesLib = updval,
},
actions: {
uploadSiteRoutes: async ({commit}, siteProps) => {
commit('setSelectedSite', siteProps)
commit('setRoutes', defaultRoutes)
},
updateRouteRow: () => ({}),
removeRoute: () => ({}),
breateAddingRoute: () => ({}),
},
namespaced: true,
}
}
})
describe("Routes", () => {
it("Not renders routes and buttons in RoutesList, if routesState value not 'active'", async () => {
const wrapper = mount(RoutesList, {
global: {
plugins: [store],
},
shallow: true,
})
expect(wrapper.html()).not.toContain('Добавить роут')
expect(wrapper.html()).not.toContain('Закрыть')
})
it("Not renders routes - empty array in RoutesList, if routesState value 'active', renders buttons", async () => {
store.commit('proxy/setRoutesState', 'active')
const getters = {...store.getters }
const routesState = getters['proxy/routesState']
const wrapper = mount(RoutesList, {
global: {
plugins: [store],
},
data() {
return {
open: false,
}
},
})
if (routesState === 'active') {
await wrapper.setData({ open: true })
}
expect(wrapper.html()).toContain('Добавить роут')
expect(wrapper.html()).toContain('Закрыть')
expect(wrapper.html()).not.toContain('ri-delete-bin-line')
})
it("Renders routes and buttons in RoutesList, if routesState value 'active' and not empty routes array", async () => {
store.commit('proxy/setRoutesState', 'active')
store.commit('proxy/setRoutes', defaultRoutes)
const getters = {...store.getters }
const routesState = getters['proxy/routesState']
const routes = getters['proxy/routes']
const wrapper = mount(RoutesList, {
global: {
plugins: [store],
},
data() {
return {
open: false,
}
},
})
console.log('routes 3 test', routes)
if (routesState === 'active') {
await wrapper.setData({ open: true })
}
console.log('wrapper 3 test', wrapper.html())
expect(wrapper.html()).toContain('Добавить роут')
expect(wrapper.html()).toContain('Закрыть')
// expect(wrapper.html()).toContain('ri-delete-bin-line')
})
})

View File

@@ -0,0 +1,306 @@
import SiteList from '@organisms/SitesEditor/SiteList.vue'
import SiteCard from '@organisms/SitesEditor/SiteCard.vue'
import EditCard from '@organisms/SitesEditor/EditCard.vue'
import Input from '@atoms/Input.vue'
import Textarea from '@atoms/Textarea.vue'
import DoubleSwitch from '@atoms/DoubleSwitch.vue'
import {mount} from '@vue/test-utils'
import {createStore} from 'vuex'
import {describe, expect, it} from 'vitest'
const defaultSites = [ {
"id": 1,
"created_at": "2024-02-22T17:08:37.715772388+03:00",
"updated_at": "2024-02-26T14:11:38.64094899+03:00",
"deleted_at": null,
"name": "jsonplaceholder.typicode.com",
"port": 9965,
"proxy_ip": "172.25.78.153",
"site_ip": "172.25.78.153",
"internet_uri": "localhost",
"description": "localhost",
"is_online": true
},
{
"id": 2,
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "2024-02-27T17:53:14.070484767+03:00",
"deleted_at": null,
"name": "new",
"port": 3645,
"proxy_ip": "172.25.78.151",
"site_ip": "172.25.78.151",
"internet_uri": "",
"description": "create... upd...",
"is_online": true
},
{
"id": 3,
"created_at": "2024-02-26T17:50:07.191225008+03:00",
"updated_at": "2024-02-28T09:41:49.274089436+03:00",
"deleted_at": null,
"name": "new",
"port": 3645,
"proxy_ip": "172.25.78.151",
"site_ip": "172.25.78.151",
"internet_uri": "",
"description": "new updated...",
"is_online": false
}]
const store = createStore({
plugins: [],
modules: {
proxy: {
state: {
sites: [],
sitesState: 'loading',
selectedSite: null,
isSaveData: false,
},
getters: {
sites: (state) => state.sites,
sitesState: (state) => state.sitesState,
selectedSite: (state) => state.selectedSite,
isSaveData: (state) => state.isSaveData,
},
mutations: {
setSites: (state, updval) => state.sites = updval,
setSitesState: (state, updval) => state.sitesState = updval,
setSelectedSite: (state, updval) => state.selectedSite = updval,
setIsSaveData: (state, updval) => state.isSaveData = updval,
},
actions: {
uploadSites({commit}, sites) {
commit('setSites', sites)
},
uploadSiteRoutes: async ({commit}, siteProps) => {
commit('setSelectedSite', siteProps)
}
},
namespaced: true,
}
}
})
describe("Services", () => {
it("Not renders SiteCard and NewSiteButton in SiteList, if sitesState value not 'active', renders SiteList, fwb-spinner", async () => {
const wrapper = mount(SiteList, {
global: {
plugins: [store],
},
shallow: true,
})
expect(wrapper.html()).not.toContain('Добавить сайт')
expect(wrapper.find(".text-slate-700").text()).toBe("Загрузка сайтов...")
})
it("Not renders fwb-spinner, SiteCard if sitesState value 'active', sites empty array, renders SiteList, NewSiteButton", async () => {
store.commit('proxy/setSitesState', 'active')
const wrapper = mount(SiteList, {
global: {
plugins: [store],
},
})
expect(wrapper.html()).not.toContain('Загрузка сайтов...')
expect(wrapper.find('span').exists()).toBe(false)
expect(wrapper.html()).toContain('Добавить сайт')
})
it("Not renders fwb-spinner, sites array not empty, sitesState value 'active', renders SiteList, NewSiteButton, SiteCard", async () => {
store.dispatch('proxy/uploadSites', defaultSites)
store.commit('proxy/setSitesState', 'active')
const wrapper = mount(SiteList, {
global: {
mocks: {
$store: store
}
},
})
expect(wrapper.find('span').exists()).toBe(true)
expect(wrapper.find('i').exists()).toBe(true)
expect(wrapper.html()).toContain('ri-pencil-line')
expect(wrapper.html()).toContain('Добавить сайт')
})
it("dynamicHeight null in SiteList component", async () => {
store.dispatch('proxy/uploadSites', defaultSites)
store.commit('proxy/setSitesState', 'active')
// const getters = {...store.getters }
// console.log('sites 3 test', getters['proxy/sites'])
const wrapper = mount(SiteList, {
global: {
mocks: {
$store: store
}
},
data() {
return {
dynamicHeight: null
}
},
shallow: true,
})
console.log('wrapper.html', wrapper.html())
expect(wrapper.classes()).not.toContain('shadow-lg')
expect(wrapper.classes()).not.toContain('p-3')
expect(wrapper.html()).not.toContain('style="max-height: 100px;"')
})
it("set dynamicHeight, this data prop not null in SiteList component", async () => {
console.log('store - 5 test', store)
store.dispatch('proxy/uploadSites', defaultSites)
store.commit('proxy/setSitesState', 'active')
const wrapper = mount(SiteList, {
global: {
plugins: [store],
},
data() {
return {
dynamicHeight: null
}
},
shallow: true,
})
await wrapper.setData({ dynamicHeight: 100 })
expect(wrapper.html()).toContain('style="max-height: 100px;"')
})
it("renders buttons - 'Отменить' and 'Удалить' in this site, after click to delete button", async () => {
const wrapper = mount(SiteCard, {
global: {
plugins: [store],
},
data () {
return {
isDelete: false,
}
},
})
await wrapper.setData({ isDelete: true })
await wrapper.get('.ri-delete-bin-line').trigger('click')
expect(wrapper.html()).toContain('Отменить')
expect(wrapper.html()).toContain('Удалить')
})
it("render EditCard, ids selectedSite and prop id is Equal", async () => {
const editSite = {
"id": 1,
"created_at": "2024-03-04T14:30:40.64845074+03:00",
"updated_at": "2024-03-04T14:30:40.64845074+03:00",
"deleted_at": null,
"name": "jsonplaceholder.typicode.com",
"port": 9965,
"proxy_ip": "172.25.78.153",
"site_ip": "https://jsonplaceholder.typicode.com/",
"internet_uri": "localhost",
"description": "localhost",
"is_online": true
}
store.dispatch('proxy/uploadSites', defaultSites)
store.commit('proxy/setSitesState', 'active')
store.dispatch('proxy/uploadSiteRoutes', editSite)
const wrapper = mount(EditCard, {
global: {
plugins: [store],
stubs: ['SiteCard', 'EditCard'],
},
props: {
id: 1,
},
})
const editCard = wrapper.getComponent(EditCard)
expect(editCard.props('id')).toEqual(editSite.id)
expect(wrapper.html()).toContain('ri-close-line')
expect(wrapper.html()).toContain('Онлайн')
})
it("render EditCard, set fields in selectedSite", async () => {
const editSite = {
"id": 1,
"created_at": "2024-03-04T14:30:40.64845074+03:00",
"updated_at": "2024-03-04T14:30:40.64845074+03:00",
"deleted_at": null,
"name": "jsonplaceholder.typicode.com",
"port": 9965,
"proxy_ip": "172.25.78.153",
"site_ip": "https://jsonplaceholder.typicode.com/",
"internet_uri": "localhost",
"description": "localhost",
"is_online": true
}
store.dispatch('proxy/uploadSites', defaultSites)
store.commit('proxy/setSitesState', 'active')
store.dispatch('proxy/uploadSiteRoutes', editSite)
expect(store.getters['proxy/selectedSite']).toEqual(editSite) // check default props values in selected site
store.commit('proxy/setSelectedSite', {...editSite, is_online: false, name: 'edit name in EditCard', description: 'edit description in EditCard'})
const wrapper = mount(EditCard, {
global: {
plugins: [store],
stubs: ['Input', 'Textarea', 'DoubleSwitch', 'EditCard'],
},
props: {
id: 1,
},
})
const editCard = wrapper.getComponent(EditCard)
const inputField = wrapper.getComponent(Input)
const textareaField = wrapper.getComponent(Textarea)
const doubleSwitchField = wrapper.getComponent(DoubleSwitch)
const nameField = inputField.props().value
const descriptionField = textareaField.props().value
const isOnlineField = doubleSwitchField.props().isCheck
const getters = {...store.getters }
const name = getters['proxy/selectedSite'].name // selected site name
const description = getters['proxy/selectedSite'].description // selected site description
const isOnline = getters['proxy/selectedSite'].is_online // selected site is_online
expect(editCard.props('id')).toEqual(editSite.id)
expect(name).toEqual(nameField)
expect(description).toEqual(descriptionField)
expect(isOnline).toEqual(isOnlineField)
})
})

View File

@@ -0,0 +1,65 @@
import { mount} from '@vue/test-utils'
import { expect, test, describe, vi } from 'vitest'
import App from '@/App.vue';
import { createStore } from 'vuex'
import {store as proxy} from '@/store/modules/proxy';
import axios from "axios";
vi.mock('axios')
describe("tests App mounted with vuex", () => {
const store = createStore({
global: {
stubs: {
}
},
plugins: [],
modules: {
proxy
},
})
axios.get.mockResolvedValue({
data: [
{
"id": 1,
"created_at": "2024-02-22T17:08:37.715772388+03:00",
"updated_at": "2024-02-26T14:11:38.64094899+03:00",
"deleted_at": null,
"name": "jsonplaceholder.typicode.com",
"port": 9965,
"proxy_ip": "172.25.78.153",
"site_ip": "172.25.78.153",
"internet_uri": "localhost",
"description": "localhost",
"is_online": true
},
{
"id": 2,
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "2024-02-26T17:38:00.255479017+03:00",
"deleted_at": null,
"name": "new",
"port": 3645,
"proxy_ip": "172.25.78.151",
"site_ip": "172.25.78.151",
"internet_uri": "",
"description": "create...",
"is_online": true
}
],
})
test('tests App mounted with vuex', async () => {
const wrapper = mount(App, {
global: {
plugins: [store]
}
})
expect(wrapper.text()).toContain('На главную')
})
})