Skip to content

Commit

Permalink
fix(frontend): fix types for test specs (#2606)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeepc committed May 29, 2023
1 parent e01ac23 commit 7d1b985
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 32 deletions.
4 changes: 2 additions & 2 deletions web/src/models/Test.model.ts
Expand Up @@ -22,7 +22,7 @@ const Test = ({
id = '',
name = '',
description = '',
specs,
specs = [],
version = 1,
serviceUnderTest: rawTrigger,
summary = {},
Expand All @@ -35,7 +35,7 @@ const Test = ({
version,
description,
createdAt,
definition: TestSpecs({ specs: specs || [] }),
definition: TestSpecs({specs}),
trigger: Trigger(rawTrigger || {}),
summary: Summary(summary),
outputs: outputs.map((rawOutput, index) => TestOutput(rawOutput, index)),
Expand Down
30 changes: 9 additions & 21 deletions web/src/models/TestSpecs.model.ts
Expand Up @@ -10,36 +10,24 @@ export type TTestSpecEntry = {
};

export type TRawTestSpecEntry = {
selector: {query: string};
selector: string;
selectorParsed: {query: string};
assertions: string[];
name: string;
};

// TechDebt: this is a temporary method to deal with the changes on the OpenAPI
// later we need to think and update our type system to deal with that
export const rawTestSpecToNewFormat = (spec : TRawTestSpecEntry) => {
return {
name: spec.name,
assertions: spec.assertions,
selector: spec.selector?.query,
selectorParsed: spec.selector,
};
};

export type TRawTestSpecs = TTestSchemas['TestSpecs'];
type TestSpecs = Model<TRawTestSpecs, {specs: TTestSpecEntry[]}>;

const TestSpecs = ({specs = []}: TRawTestSpecs): TestSpecs => {
const newSpecs = specs.map(({selectorParsed: {query = ''} = {}, assertions = [], name = ''}) => ({
assertions,
isDeleted: false,
isDraft: false,
selector: query,
name: name ?? '',
}));

return {
specs: newSpecs
specs: specs.map(({selector = '', assertions = [], name = ''}) => ({
assertions,
isDeleted: false,
isDraft: false,
selector,
name: name ?? '',
})),
};
};

Expand Down
7 changes: 2 additions & 5 deletions web/src/redux/actions/TestSpecs.actions.ts
Expand Up @@ -10,7 +10,7 @@ import TestService from 'services/Test.service';
import TestRun from 'models/TestRun.model';
import Test from 'models/Test.model';
import AssertionResults from 'models/AssertionResults.model';
import {TTestSpecEntry, rawTestSpecToNewFormat} from 'models/TestSpecs.model';
import {TTestSpecEntry} from 'models/TestSpecs.model';
import {RootState} from '../store';

export type TChange = {
Expand All @@ -35,10 +35,7 @@ const TestSpecsActions = () => ({
({definitionList, testId, runId}, {dispatch}) => {
const specs = definitionList.map(def => TestDefinitionService.toRaw(def));

// TODO: improve type system later to deal with the new TestSpec type
const newSpecs = specs.map(rawTestSpecToNewFormat);

return dispatch(TestRunGateway.dryRun(testId, runId, { specs: newSpecs })).unwrap();
return dispatch(TestRunGateway.dryRun(testId, runId, {specs})).unwrap();
}
),
});
Expand Down
3 changes: 1 addition & 2 deletions web/src/services/Test.service.ts
Expand Up @@ -6,7 +6,6 @@ import {IPlugin} from 'types/Plugins.types';
import {TDraftTest} from 'types/Test.types';
import Validator from 'utils/Validator';
import Test, {TRawTest} from 'models/Test.model';
import { rawTestSpecToNewFormat } from 'models/TestSpecs.model';
import TestDefinitionService from './TestDefinition.service';
import GrpcService from './Triggers/Grpc.service';
import HttpService from './Triggers/Http.service';
Expand Down Expand Up @@ -63,7 +62,7 @@ const TestService = () => ({
...(original
? {
outputs: toRawTestOutputs(original.outputs ?? []),
specs: original.definition.specs.map(def => rawTestSpecToNewFormat(TestDefinitionService.toRaw(def))),
specs: original.definition.specs.map(def => TestDefinitionService.toRaw(def)),
}
: {}),
};
Expand Down
3 changes: 2 additions & 1 deletion web/src/services/TestDefinition.service.ts
Expand Up @@ -3,7 +3,8 @@ import {TRawTestSpecEntry, TTestSpecEntry} from 'models/TestSpecs.model';
const TestDefinitionService = () => ({
toRaw({selector, assertions, name}: TTestSpecEntry): TRawTestSpecEntry {
return {
selector: {query: selector},
selector,
selectorParsed: {query: selector},
assertions,
name,
};
Expand Down
3 changes: 2 additions & 1 deletion web/src/services/__tests__/TestDefinition.service.test.ts
Expand Up @@ -13,7 +13,8 @@ describe('TestDefinitionService', () => {
});
expect(testResultCount).toEqual({
assertions: [],
selector: {
selector: '',
selectorParsed: {
query: '',
},
name: '',
Expand Down

0 comments on commit 7d1b985

Please sign in to comment.