Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(frontend): fix types for test specs #2606

Merged
merged 2 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions web/src/models/Test.model.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe('TestDefinitionService', () => {
});
expect(testResultCount).toEqual({
assertions: [],
selector: {
selector: '',
selectorParsed: {
query: '',
},
name: '',
Expand Down