From 4699705650fff806249b65b13cad78c044b278fe Mon Sep 17 00:00:00 2001 From: Timon Back Date: Fri, 3 May 2024 13:49:56 +0200 Subject: [PATCH] test: add basic tests on the data displayed in the component --- .../channel-main.component.spec.ts | 28 ++++++------------ .../channels/channels.component.spec.ts | 27 +++++++++-------- .../app/components/mock-components.spec.ts | 29 +++++++++++++++++++ .../servers/servers.component.spec.ts | 13 +++++++-- 4 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 springwolf-ui/src/app/components/mock-components.spec.ts diff --git a/springwolf-ui/src/app/components/channels/channel-main/channel-main.component.spec.ts b/springwolf-ui/src/app/components/channels/channel-main/channel-main.component.spec.ts index ae761813e..bdec5b44b 100644 --- a/springwolf-ui/src/app/components/channels/channel-main/channel-main.component.spec.ts +++ b/springwolf-ui/src/app/components/channels/channel-main/channel-main.component.spec.ts @@ -4,28 +4,16 @@ import { ChannelMainComponent } from "./channel-main.component"; import { AsyncApiService } from "../../../service/asyncapi/asyncapi.service"; import { PublisherService } from "../../../service/publisher.service"; import { MaterialModule } from "../../../material.module"; -import { of } from "rxjs"; -import { exampleSchemas } from "../../../service/mock/example-data"; -import { AsyncApiMapperService } from "../../../service/asyncapi/asyncapi-mapper.service"; import { MarkdownModule } from "ngx-markdown"; import { mockedAsyncApiService, mockedExampleSchemaMapped, } from "../../../service/mock/mock-asyncapi.service"; -import { Component, Input } from "@angular/core"; -import { Schema } from "../../../models/schema.model"; - -@Component({ selector: "app-json", template: "" }) -export class MockAppJson { - @Input() data: any; -} - -@Component({ selector: "app-schema", template: "" }) -export class MockAppSchema { - @Input() schema: Schema; -} +import { MockAppJson, MockAppSchema } from "../../mock-components.spec"; describe("ChannelMainComponent", () => { + const mockData = mockedExampleSchemaMapped.channelOperations[0]; + beforeEach(async () => { mockedAsyncApiService.getAsyncApi.mockClear(); @@ -37,13 +25,15 @@ describe("ChannelMainComponent", () => { { provide: PublisherService, useValue: {} }, ], componentProperties: { - channelName: mockedExampleSchemaMapped.channelOperations[0].name, - operation: mockedExampleSchemaMapped.channelOperations[0].operation, + channelName: mockData.name, + operation: mockData.operation, }, }); }); - it("should create the component", () => { - expect(screen.getByText("Another payload model")).toBeTruthy(); + it("should render the component and data", () => { + expect( + screen.getByText(mockData.operation.message.description) + ).toBeTruthy(); }); }); diff --git a/springwolf-ui/src/app/components/channels/channels.component.spec.ts b/springwolf-ui/src/app/components/channels/channels.component.spec.ts index f250587d7..107616bfc 100644 --- a/springwolf-ui/src/app/components/channels/channels.component.spec.ts +++ b/springwolf-ui/src/app/components/channels/channels.component.spec.ts @@ -2,20 +2,12 @@ import { render, screen } from "@testing-library/angular"; import { ChannelsComponent } from "./channels.component"; import { AsyncApiService } from "../../service/asyncapi/asyncapi.service"; -import { MatAccordion } from "@angular/material/expansion"; -import { mockedAsyncApiService } from "../../service/mock/mock-asyncapi.service"; +import { + mockedAsyncApiService, + mockedExampleSchemaMapped, +} from "../../service/mock/mock-asyncapi.service"; import { MaterialModule } from "../../material.module"; -import { Operation } from "../../models/operation.model"; -import { Component, Input } from "@angular/core"; - -@Component({ - selector: "app-channel-main", - template: "", -}) -export class MockChannelMainComponent { - @Input() channelName: string; - @Input() operation: Operation; -} +import { MockChannelMainComponent } from "../mock-components.spec"; describe("ChannelsComponent", () => { beforeEach(async () => { @@ -30,7 +22,14 @@ describe("ChannelsComponent", () => { }); }); - it("should create the component", () => { + it("should render the component and data", () => { expect(screen.getByText("Channels")).toBeTruthy(); + + mockedExampleSchemaMapped.channelOperations.forEach((channelOperation) => { + expect(screen.getAllByText(channelOperation.name)[0]).toBeTruthy(); + expect( + screen.getAllByText(channelOperation.operation.message.title)[0] + ).toBeTruthy(); + }); }); }); diff --git a/springwolf-ui/src/app/components/mock-components.spec.ts b/springwolf-ui/src/app/components/mock-components.spec.ts new file mode 100644 index 000000000..299ffbb0c --- /dev/null +++ b/springwolf-ui/src/app/components/mock-components.spec.ts @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +import { Component, Input } from "@angular/core"; +import { Schema } from "../models/schema.model"; +import { Operation } from "../models/operation.model"; + +/* + * This file contains all mock components for usage in tests. + */ +describe("MockTest", () => { + it("basic test, since at least one test is required 1 + 1 = 2", () => { + expect(1 + 1).toBe(2); + }); +}); + +@Component({ selector: "app-json", template: "" }) +export class MockAppJson { + @Input() data: any; +} + +@Component({ selector: "app-schema", template: "" }) +export class MockAppSchema { + @Input() schema: Schema; +} + +@Component({ selector: "app-channel-main", template: "" }) +export class MockChannelMainComponent { + @Input() channelName: string; + @Input() operation: Operation; +} diff --git a/springwolf-ui/src/app/components/servers/servers.component.spec.ts b/springwolf-ui/src/app/components/servers/servers.component.spec.ts index 2f9e82963..9a5b7429b 100644 --- a/springwolf-ui/src/app/components/servers/servers.component.spec.ts +++ b/springwolf-ui/src/app/components/servers/servers.component.spec.ts @@ -2,7 +2,10 @@ import { render, screen } from "@testing-library/angular"; import { ServersComponent } from "./servers.component"; import { AsyncApiService } from "../../service/asyncapi/asyncapi.service"; -import { mockedAsyncApiService } from "../../service/mock/mock-asyncapi.service"; +import { + mockedAsyncApiService, + mockedExampleSchemaMapped, +} from "../../service/mock/mock-asyncapi.service"; import { MaterialModule } from "../../material.module"; describe("ServerComponent", () => { @@ -17,7 +20,13 @@ describe("ServerComponent", () => { }); }); - it("should create the component", () => { + it("should render the component and data", () => { expect(screen.getByText("Servers")).toBeTruthy(); + + mockedExampleSchemaMapped.servers.forEach((server, key) => { + expect(screen.getAllByText(key)[0]).toBeTruthy(); + expect(screen.getAllByText(server.protocol)[0]).toBeTruthy(); + expect(screen.getAllByText(server.host)[0]).toBeTruthy(); + }); }); });