-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
141 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 116 additions & 58 deletions
174
src/composables/external-tool-mappings.composable.unit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,132 @@ | ||
import { | ||
SchoolExternalToolResponse, | ||
SchoolExternalToolSearchListResponse, | ||
} from "@/serverApi/v3"; | ||
import { useExternalToolMappings } from "./external-tool-mappings.composable"; | ||
import { BusinessError } from "@/store/types/commons"; | ||
import { schoolToolConfigurationStatusFactory } from "@@/tests/test-utils/factory"; | ||
|
||
describe("useExternalToolUtils", () => { | ||
const setup = () => { | ||
const { getBusinessErrorTranslationKey } = useExternalToolMappings(); | ||
|
||
const toolResponse: SchoolExternalToolResponse = { | ||
id: "id", | ||
name: "toolName", | ||
toolId: "toolId", | ||
toolVersion: 1, | ||
schoolId: "schoolId", | ||
parameters: [ | ||
{ | ||
name: "name", | ||
value: "value", | ||
}, | ||
], | ||
status: schoolToolConfigurationStatusFactory.build(), | ||
}; | ||
|
||
const listResponse: SchoolExternalToolSearchListResponse = { | ||
data: [toolResponse], | ||
}; | ||
|
||
return { | ||
listResponse, | ||
toolResponse, | ||
getBusinessErrorTranslationKey, | ||
}; | ||
}; | ||
|
||
describe("useExternalToolMappings", () => { | ||
describe("getBusinessErrorTranslationKey", () => { | ||
it("should return original message when key is undefined", () => { | ||
const { getBusinessErrorTranslationKey } = setup(); | ||
describe("when error type was found", () => { | ||
const setup = () => { | ||
const { getBusinessErrorTranslationKey } = useExternalToolMappings(); | ||
|
||
const translationKey: string | undefined = | ||
getBusinessErrorTranslationKey(undefined); | ||
expect(translationKey).toBeUndefined(); | ||
const key = "pages.tool.apiError.tool_with_name_exists"; | ||
const apiError = { | ||
message: | ||
"A tool with the same name is already assigned to this course. Tool names must be unique within a course.", | ||
code: 400, | ||
title: "Toolname already exists", | ||
type: "CONTEXT_EXTERNAL_TOOL_NAME_ALREADY_EXISTS", | ||
}; | ||
const businessError: BusinessError = { | ||
statusCode: apiError.code, | ||
message: apiError.message, | ||
error: apiError, | ||
}; | ||
|
||
return { getBusinessErrorTranslationKey, businessError, key }; | ||
}; | ||
|
||
it("should return translation key", () => { | ||
const { getBusinessErrorTranslationKey, businessError, key } = setup(); | ||
|
||
const translationKey: string | undefined = | ||
getBusinessErrorTranslationKey(businessError); | ||
expect(translationKey).toEqual(key); | ||
}); | ||
}); | ||
|
||
describe("when translation key was not found", () => { | ||
const setup = () => { | ||
const { getBusinessErrorTranslationKey } = useExternalToolMappings(); | ||
|
||
const apiError = { | ||
message: "message", | ||
code: 400, | ||
title: "title", | ||
type: "some_key", | ||
}; | ||
const businessError: BusinessError = { | ||
statusCode: apiError.code, | ||
message: apiError.message, | ||
error: apiError, | ||
}; | ||
|
||
return { getBusinessErrorTranslationKey, businessError }; | ||
}; | ||
|
||
it("should return original error message", () => { | ||
const { getBusinessErrorTranslationKey, businessError } = setup(); | ||
|
||
const translationKey: string | undefined = | ||
getBusinessErrorTranslationKey(businessError); | ||
expect(translationKey).toEqual(businessError.message); | ||
}); | ||
}); | ||
|
||
describe("when businessError.error is undefined", () => { | ||
const setup = () => { | ||
const { getBusinessErrorTranslationKey } = useExternalToolMappings(); | ||
|
||
const businessError: BusinessError = { | ||
statusCode: 400, | ||
message: "original message", | ||
error: undefined, | ||
}; | ||
|
||
return { getBusinessErrorTranslationKey, businessError }; | ||
}; | ||
|
||
it("should return original error message", () => { | ||
const { getBusinessErrorTranslationKey, businessError } = setup(); | ||
|
||
const translationKey: string | undefined = | ||
getBusinessErrorTranslationKey(businessError); | ||
|
||
expect(translationKey).toEqual(businessError.message); | ||
}); | ||
}); | ||
|
||
it("should return translation key when message was found", () => { | ||
const { getBusinessErrorTranslationKey } = setup(); | ||
const error: BusinessError = { | ||
statusCode: "400", | ||
message: "tool_param_duplicate: Some validationError was thrown", | ||
describe("when error.type doesn't exist", () => { | ||
const setup = () => { | ||
const { getBusinessErrorTranslationKey } = useExternalToolMappings(); | ||
|
||
const apiError = { | ||
message: "message", | ||
code: 400, | ||
title: "title", | ||
}; | ||
const businessError: BusinessError = { | ||
statusCode: apiError.code, | ||
message: apiError.message, | ||
error: apiError, | ||
}; | ||
|
||
return { getBusinessErrorTranslationKey, businessError }; | ||
}; | ||
|
||
const translationKey: string | undefined = | ||
getBusinessErrorTranslationKey(error); | ||
expect(translationKey).toEqual( | ||
"pages.tool.apiError.tool_param_duplicate" | ||
); | ||
it("should return original error message", () => { | ||
const { getBusinessErrorTranslationKey, businessError } = setup(); | ||
|
||
const translationKey: string | undefined = | ||
getBusinessErrorTranslationKey(businessError); | ||
expect(translationKey).toEqual(businessError.message); | ||
}); | ||
}); | ||
|
||
it("should return original message when key was not found", () => { | ||
const { getBusinessErrorTranslationKey } = setup(); | ||
const error: BusinessError = { | ||
statusCode: "400", | ||
message: "some_error: which is not defined in map", | ||
describe("when businessError is undefined", () => { | ||
const setup = () => { | ||
const { getBusinessErrorTranslationKey } = useExternalToolMappings(); | ||
|
||
return { | ||
getBusinessErrorTranslationKey, | ||
}; | ||
}; | ||
|
||
const translationKey: string | undefined = | ||
getBusinessErrorTranslationKey(error); | ||
expect(translationKey).toEqual(error.message); | ||
it("should return undefined", () => { | ||
const { getBusinessErrorTranslationKey } = setup(); | ||
|
||
const translationKey: string | undefined = | ||
getBusinessErrorTranslationKey(undefined); | ||
expect(translationKey).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters