From 445b9e2e2ac0728c427c84942ac09117ccc6f87f Mon Sep 17 00:00:00 2001 From: Deniz Kusefoglu Date: Wed, 6 Mar 2019 00:19:28 -0800 Subject: [PATCH] Add testst to resourcesToTemplate --- .../shared/utils/resourceToTemplate.test.ts | 123 ++++++++++++++++++ ui/src/shared/utils/resourceToTemplate.ts | 4 +- 2 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 ui/src/shared/utils/resourceToTemplate.test.ts diff --git a/ui/src/shared/utils/resourceToTemplate.test.ts b/ui/src/shared/utils/resourceToTemplate.test.ts new file mode 100644 index 0000000000..b8511140aa --- /dev/null +++ b/ui/src/shared/utils/resourceToTemplate.test.ts @@ -0,0 +1,123 @@ +import { + labelToRelationship, + labelToIncluded, + taskToTemplate, +} from 'src/shared/utils/resourceToTemplate' +import {TemplateType, Task as TaskApi} from '@influxdata/influx' +import {Label, Task} from 'src/types/v2' + +const myfavelabel: Label = { + id: '1', + name: '1label', + properties: {color: 'fffff', description: 'omg'}, +} + +const myfavetask: Task = { + authorizationID: '037b084ed9abc000', + every: '24h0m0s', + flux: + 'option task = {name: "lala", every: 86400000000000ns, offset: 60000000000ns}\n\nfrom(bucket: "defnuck")\n\t|> range(start: -task.every)', + id: '037b0877b359a000', + labels: [ + { + id: '037b0c86a92a2000', + name: 'yum', + properties: {color: '#FF8564', description: ''}, + }, + ], + name: 'lala', + offset: '1m0s', + org: 'org', + orgID: '037b084ec8ebc000', + status: TaskApi.StatusEnum.Active, +} + +describe('resourceToTemplate', () => { + describe('labelToRelationship', () => { + it('converts a label to a relationship struct', () => { + const actual = labelToRelationship(myfavelabel) + const expected = {type: TemplateType.Label, id: myfavelabel.id} + + expect(actual).toEqual(expected) + }) + }) + describe('labelToIncluded', () => { + it('converts a label to a data structure in included', () => { + const actual = labelToIncluded(myfavelabel) + const expected = { + type: TemplateType.Label, + id: myfavelabel.id, + attributes: { + name: myfavelabel.name, + properties: { + color: myfavelabel.properties.color, + description: myfavelabel.properties.description, + }, + }, + } + + expect(actual).toEqual(expected) + }) + }), + describe('taskToTemplate', () => { + it('converts a task to a template', () => { + const actual = taskToTemplate(myfavetask) + const expected = { + content: { + data: { + type: 'task', + attributes: { + every: '24h0m0s', + flux: + 'option task = {name: "lala", every: 86400000000000ns, offset: 60000000000ns}\n\nfrom(bucket: "defnuck")\n\t|> range(start: -task.every)', + name: 'lala', + offset: '1m0s', + status: 'active', + }, + relationships: { + label: { + data: [ + { + id: '037b0c86a92a2000', + type: 'label', + }, + ], + }, + }, + }, + included: [ + { + attributes: { + name: 'yum', + properties: { + color: '#FF8564', + description: '', + }, + }, + id: '037b0c86a92a2000', + type: TemplateType.Label, + }, + ], + }, + labels: [ + { + id: '1', + name: 'influx.task', + properties: { + color: 'ffb3b3', + description: + 'This is a template for a task resource on influx 2.0', + }, + }, + ], + meta: { + description: 'template created from task: lala', + name: 'lala-Template', + version: '1', + }, + } + + expect(actual).toEqual(expected) + }) + }) +}) diff --git a/ui/src/shared/utils/resourceToTemplate.ts b/ui/src/shared/utils/resourceToTemplate.ts index 087d729537..ef7e7a234a 100644 --- a/ui/src/shared/utils/resourceToTemplate.ts +++ b/ui/src/shared/utils/resourceToTemplate.ts @@ -33,11 +33,11 @@ const blankTaskTemplate = { ], } -const labelToRelationship = (l: Label) => { +export const labelToRelationship = (l: Label) => { return {type: TemplateType.Label, id: l.id} } -const labelToIncluded = (l: Label) => { +export const labelToIncluded = (l: Label) => { return { type: TemplateType.Label, id: l.id,