265 lines
6.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { expect, test } from 'vitest'
import Adapter from '../src/adapter'
const { convertObject, convertList } = Adapter
const appBook = {
"id": "fb5e7d1d-38cd-4831-bae9-07b36080e3e7",
"createdAt": "2024-03-01T23:47:35.711668+03:00",
"updatedAt": "2024-03-01T23:47:35.711668+03:00",
"deletedAt": null,
"owner": "e75aae0d-c1eb-4199-a1d8-2177f57d6a1e",
"title": "Тестовая книга",
"author": "Васильев А.В.",
"description": "test description",
"isPublic": false,
"publication": null,
"imageLink": null,
"mapLink": null,
"mapParamsId": null,
"variables": []
}
const apiBook = {
"id": "fb5e7d1d-38cd-4831-bae9-07b36080e3e7",
"created_at": "2024-03-01T23:47:35.711668+03:00",
"updated_at": "2024-03-01T23:47:35.711668+03:00",
"deleted_at": null,
"owner": "e75aae0d-c1eb-4199-a1d8-2177f57d6a1e",
"title": "Тестовая книга",
"author": "Васильев А.В.",
"description": "test description",
"is_public": false,
"publication": null,
"image_link": null,
"map_link": null,
"map_params_id": null,
"variables": []
}
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 config = {
id: "id",
name: "name2",
port: "port2",
proxy_ip: "proxyIp",
site_ip: "siteIp",
internet_uri: "internetUri",
description: "description",
is_online: "isOnline",
}
const expectedObject = {
id: 1,
name2: "jsonplaceholder.typicode.com",
port2: 9965,
proxyIp: "172.25.78.153",
siteIp: "172.25.78.36",
internetUri: "localhost",
description: "localhost",
isOnline: true,
}
expect(convertObject(targetObject, {config})).toEqual(expectedObject)
})
test('parse book object', () => {
const targetObject = {...apiBook}
const expectedObject = {...appBook}
const config = {
id: 'id',
created_at: 'createdAt',
updated_at: 'updatedAt',
deleted_at: 'deletedAt',
owner: 'owner',
title: 'title',
author: 'author',
description: 'description',
is_public: 'isPublic',
publication: 'publication',
image_link: 'imageLink',
map_link: 'mapLink',
map_params_id: 'mapParamsId',
variables: 'variables'
}
const newObject = convertObject(targetObject, {config})
expect(newObject).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)
})