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

[CP-2948] add conditional mock responses #1965

Merged
merged 8 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions apps/mudita-center-e2e/src/specs/overview/e2e-mock-match.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { E2EMockClient } from "../../../../../libs/e2e-mock/client/src"
import {
apiConfigWithContacts,
contactsConfig,
contactsData,
menuWithContacts,
} from "../../../../../libs/e2e-mock/responses/src"

describe("E2E mock match sample", () => {
before(async () => {
E2EMockClient.connect()
//wait for a connection to be established
await browser.waitUntil(() => {
return E2EMockClient.checkConnection()
})
})

after(() => {
E2EMockClient.stopServer()
E2EMockClient.disconnect()
})

it.only("Connect device with two different responses for one endpoint", async () => {
// if body of FEATURE_CONFIGURATION request is equal to { feature: "contacts", lang: "en-US" } then return body: contactsConfig.
// in other cases return response without match filed
E2EMockClient.mockResponse({
path: "path-1",
body: contactsConfig,
endpoint: "FEATURE_CONFIGURATION",
method: "GET",
status: 200,
match: {
expected: { feature: "contacts", lang: "en-US" },
},
})
// if body of FEATURE_DATA request is equal to { feature: "contacts", lang: "en-US" } then return body: contactsConfig.
// in other cases return response without match filed
E2EMockClient.mockResponse({
path: "path-1",
body: contactsData,
endpoint: "FEATURE_DATA",
method: "GET",
status: 200,
match: {
expected: { feature: "contacts", lang: "en-US" },
},
})
E2EMockClient.mockResponse({
path: "path-1",
body: apiConfigWithContacts,
endpoint: "API_CONFIGURATION",
method: "GET",
status: 200,
})
E2EMockClient.mockResponse({
path: "path-1",
body: menuWithContacts,
endpoint: "MENU_CONFIGURATION",
method: "GET",
status: 200,
})

E2EMockClient.addDevice({
path: "path-1",
serialNumber: "first-serial-number",
})
await browser.pause(6000)
const menuItem = await $(`//a[@href="#/generic/contacts"]`)

await menuItem.waitForDisplayed({ timeout: 10000 })
await expect(menuItem).toBeDisplayed()

await browser.pause(10000)
})
})
5 changes: 2 additions & 3 deletions libs/device/feature/src/lib/api-config/api-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ export class APIConfigService {
if (!device) {
return Result.failed(new AppError(GeneralError.NoDevice, ""))
}

const response = await device.request({
endpoint: "API_CONFIGURATION",
method: "GET",
body: {},
options: {
connectionTimeOut: 1000
}
connectionTimeOut: 1000,
},
})

if (response.ok) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class APIFeaturesService {
lang: "en-US",
},
})

if (response.ok) {
const config = featureConfigValidator.safeParse(response.data.body)
if (process.env.NODE_ENV === "development" && !config.success) {
Expand Down Expand Up @@ -107,6 +108,7 @@ export class APIFeaturesService {
lang: "en-US",
},
})

if (response.ok) {
return Result.success(response.data.body)
}
Expand Down
3 changes: 3 additions & 0 deletions libs/e2e-mock/responses/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
export * from "./lib/default-responses"
export * from "./lib/outbox-responses"
export * from "./lib/overview-responses"
export * from "./lib/menu-responses"
export * from "./lib/api-config-responses"
export * from "./lib/contacts-responses"
16 changes: 16 additions & 0 deletions libs/e2e-mock/responses/src/lib/api-config-responses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { ApiConfig } from "device/models"

export const apiConfigWithContacts: ApiConfig = {
apiVersion: "1.0.0",
lang: "en-US",
variant: "black",
features: ["mc-overview", "contacts"],
productId: "2006",
vendorId: "0e8d",
serialNumber: "0123456789ABCDEF",
}
47 changes: 47 additions & 0 deletions libs/e2e-mock/responses/src/lib/contacts-responses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

export const contactsData = {}

export const contactsConfig = {
main: {
screenTitle: "Contacts",
component: "block-plain",
childrenKeys: ["fullScreenWrapper"],
layout: {
flexLayout: {
direction: "column",
alignItems: "center",
justifyContent: "center",
},
},
},
fullScreenWrapper: {
component: "block-plain",
childrenKeys: ["title", "detailText", "importContactsButton"],
layout: {
flexLayout: {
direction: "column",
alignItems: "center",
justifyContent: "center",
rowGap: "8px",
columnGap: "8px",
},
},
},
title: {
component: "h3-component",
config: { text: "Import your contacts" },
},
detailText: {
component: "p1-component",
config: { text: "Import all your contacts from a singles ource." },
},
importContactsButton: {
component: "mc-import-contacts-button",
config: { text: "import contacts" },
layout: { margin: "16px" },
},
}
9 changes: 8 additions & 1 deletion libs/e2e-mock/responses/src/lib/default-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { ApiResponse } from "Core/device/types/mudita-os"
import { MatchConfig } from "Libs/e2e-mock/server/src"
import { APIEndpointType, APIMethodsType } from "device/models"

//import from "Core/device" breaks usage in e2e
Expand All @@ -29,9 +30,15 @@ enum ResponseStatus {
Timeout = 505,
}

export type ApiResponseWithConfig = ApiResponse<unknown> & {
match?: MatchConfig
}

export type ApiResponsesWithConfigArray = ApiResponseWithConfig[]

type MethodObject = Partial<Record<APIMethodsType, ApiResponse<unknown>>>

type MethodArray = Partial<Record<APIMethodsType, ApiResponse<unknown>[]>>
type MethodArray = Partial<Record<APIMethodsType, ApiResponsesWithConfigArray>>

export type MockResponsesMap = Partial<Record<APIEndpointType, MethodObject>>
export type MocksArrayResponsesMap = Partial<
Expand Down
20 changes: 20 additions & 0 deletions libs/e2e-mock/responses/src/lib/menu-responses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

export const menuWithContacts = {
title: "Kompakt",
menuItems: [
{
feature: "mc-overview",
displayName: "Overview",
icon: "overview",
},
{
feature: "contacts",
displayName: "Contacts",
icon: "contacts-book",
},
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ export const addKompaktValidator = z.object({
serialNumber: z.string().min(1),
})

const MatchConfigValidator = z.object({
expected: z.object({}).passthrough(),
options: z
.object({
id: z.string().optional(),
})
.optional(),
})

export type MatchConfig = z.infer<typeof MatchConfigValidator>

export type AddKompakt = z.infer<typeof addKompaktValidator>

export const addKompaktResponseValidator = z.object({
Expand All @@ -20,6 +31,7 @@ export const addKompaktResponseValidator = z.object({
method: z.enum(APIMethods),
status: z.nativeEnum(ResponseStatus),
body: z.object({}).passthrough(),
match: MatchConfigValidator.optional(),
})

export type AddKompaktResponse = z.infer<typeof addKompaktResponseValidator>
Loading
Loading