This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 829
OIDC: unit test ServerPickerDialog #11019
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
275009f
add aria-label to default homeserver checkbox
79098c6
test ServerPickerDialog
b015f22
remove debug
4a6de33
strict fixes
6741f33
Merge branch 'develop' into kerry/25472/test-ServerPickerDialog
fd21bfe
use testid instead of aria-label
1e57560
i18n
61ba3fe
Merge branch 'develop' into kerry/25472/test-ServerPickerDialog
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
241 changes: 241 additions & 0 deletions
241
test/components/views/dialogs/ServerPickerDialog-test.tsx
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 |
---|---|---|
@@ -0,0 +1,241 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import React from "react"; | ||
import { fireEvent, render, screen } from "@testing-library/react"; | ||
import fetchMock from "fetch-mock-jest"; | ||
|
||
import ServerPickerDialog from "../../../../src/components/views/dialogs/ServerPickerDialog"; | ||
import SdkConfig from "../../../../src/SdkConfig"; | ||
import { flushPromises } from "../../../test-utils"; | ||
import { ValidatedServerConfig } from "../../../../src/utils/ValidatedServerConfig"; | ||
|
||
// Fake random strings to give a predictable snapshot for IDs | ||
jest.mock("matrix-js-sdk/src/randomstring", () => ({ | ||
randomString: () => "abdefghi", | ||
})); | ||
|
||
describe("<ServerPickerDialog />", () => { | ||
const defaultServerConfig = { | ||
hsUrl: "https://matrix.org", | ||
hsName: "matrix.org", | ||
hsNameIsDifferent: true, | ||
isUrl: "https://is.org", | ||
isDefault: true, | ||
isNameResolvable: true, | ||
warning: "", | ||
}; | ||
const wkHsUrl = "https://hsbaseurlfrom.wk"; | ||
const wkIsUrl = "https://isbaseurlfrom.wk"; | ||
const validWellKnown = { | ||
"m.homeserver": { | ||
base_url: wkHsUrl, | ||
}, | ||
"m.identity_server": { | ||
base_url: wkIsUrl, | ||
}, | ||
}; | ||
const defaultProps = { | ||
serverConfig: defaultServerConfig, | ||
onFinished: jest.fn(), | ||
}; | ||
const getComponent = ( | ||
props: Partial<{ | ||
onFinished: any; | ||
serverConfig: ValidatedServerConfig; | ||
}> = {}, | ||
) => render(<ServerPickerDialog {...defaultProps} {...props} />); | ||
|
||
beforeEach(() => { | ||
SdkConfig.add({ | ||
validated_server_config: defaultServerConfig, | ||
}); | ||
|
||
fetchMock.resetHistory(); | ||
}); | ||
|
||
it("should render dialog", () => { | ||
const { container } = getComponent(); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
// checkbox and text input have the same aria-label | ||
const getOtherHomeserverCheckBox = () => | ||
screen.getAllByLabelText("Other homeserver").find((node) => node.getAttribute("type") === "radio")!; | ||
const getOtherHomeserverInput = () => | ||
screen.getAllByLabelText("Other homeserver").find((node) => node.getAttribute("type") === "text")!; | ||
|
||
describe("when default server config is selected", () => { | ||
it("should select other homeserver field on open", () => { | ||
getComponent(); | ||
expect(getOtherHomeserverCheckBox()).toBeChecked(); | ||
// empty field | ||
expect(getOtherHomeserverInput()).toHaveDisplayValue(""); | ||
}); | ||
|
||
it("should display an error when trying to continue with an empty homeserver field", async () => { | ||
const onFinished = jest.fn(); | ||
const { container } = getComponent({ onFinished }); | ||
|
||
fireEvent.click(screen.getByText("Continue")); | ||
|
||
await flushPromises(); | ||
|
||
// error on field | ||
expect(container.querySelector(".mx_ServerPickerDialog_otherHomeserver.mx_Field_invalid")).toBeTruthy(); | ||
|
||
// didn't close dialog | ||
expect(onFinished).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should close when selecting default homeserver and clicking continue", async () => { | ||
const onFinished = jest.fn(); | ||
getComponent({ onFinished }); | ||
|
||
fireEvent.click(screen.getByLabelText("Default homeserver")); | ||
expect(screen.getByLabelText("Default homeserver")).toBeChecked(); | ||
|
||
fireEvent.click(screen.getByText("Continue")); | ||
|
||
// serverpicker still validates the 'other homeserver' field on submit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logged a bug for this element-hq/element-web#25488 |
||
// when default is chosen | ||
// so this throws a lot of errors into the console | ||
// and is asynchronous while waiting for validation | ||
await flushPromises(); | ||
|
||
// closed dialog with default server | ||
expect(onFinished).toHaveBeenCalledWith(defaultServerConfig); | ||
}); | ||
|
||
it("should submit successfully with a valid custom homeserver", async () => { | ||
const homeserver = "https://myhomeserver.site"; | ||
fetchMock.get(`${homeserver}/_matrix/client/versions`, { | ||
unstable_features: {}, | ||
versions: [], | ||
}); | ||
const onFinished = jest.fn(); | ||
getComponent({ onFinished }); | ||
|
||
fireEvent.change(getOtherHomeserverInput(), { target: { value: homeserver } }); | ||
expect(getOtherHomeserverInput()).toHaveDisplayValue(homeserver); | ||
|
||
fireEvent.click(screen.getByText("Continue")); | ||
|
||
// validation on submit is async | ||
await flushPromises(); | ||
|
||
// closed dialog with validated custom server | ||
expect(onFinished).toHaveBeenCalledWith({ | ||
hsName: "myhomeserver.site", | ||
hsUrl: homeserver, | ||
hsNameIsDifferent: false, | ||
warning: null, | ||
isDefault: false, | ||
isNameResolvable: false, | ||
isUrl: defaultServerConfig.isUrl, | ||
}); | ||
}); | ||
|
||
describe("validates custom homeserver", () => { | ||
it("should lookup .well-known for homeserver without protocol", async () => { | ||
const homeserver = "myhomeserver1.site"; | ||
const wellKnownUrl = `https://${homeserver}/.well-known/matrix/client`; | ||
fetchMock.get(wellKnownUrl, {}); | ||
getComponent(); | ||
|
||
fireEvent.change(getOtherHomeserverInput(), { target: { value: homeserver } }); | ||
expect(getOtherHomeserverInput()).toHaveDisplayValue(homeserver); | ||
// trigger validation | ||
fireEvent.blur(getOtherHomeserverInput()); | ||
|
||
// validation on submit is async | ||
await flushPromises(); | ||
|
||
expect(fetchMock).toHaveFetched(wellKnownUrl); | ||
}); | ||
|
||
it("should submit using validated config from a valid .well-known", async () => { | ||
const homeserver = "myhomeserver2.site"; | ||
const wellKnownUrl = `https://${homeserver}/.well-known/matrix/client`; | ||
|
||
// urls from homeserver well-known | ||
const versionsUrl = `${wkHsUrl}/_matrix/client/versions`; | ||
const isWellKnownUrl = `${wkIsUrl}/_matrix/identity/v2`; | ||
|
||
fetchMock.getOnce(wellKnownUrl, validWellKnown); | ||
fetchMock.getOnce(versionsUrl, { | ||
versions: [], | ||
}); | ||
fetchMock.getOnce(isWellKnownUrl, {}); | ||
const onFinished = jest.fn(); | ||
getComponent({ onFinished }); | ||
|
||
fireEvent.change(getOtherHomeserverInput(), { target: { value: homeserver } }); | ||
fireEvent.click(screen.getByText("Continue")); | ||
|
||
// validation on submit is async | ||
await flushPromises(); | ||
|
||
expect(fetchMock).toHaveFetched(wellKnownUrl); | ||
// fetched using urls from .well-known | ||
expect(fetchMock).toHaveFetched(versionsUrl); | ||
expect(fetchMock).toHaveFetched(isWellKnownUrl); | ||
|
||
expect(onFinished).toHaveBeenCalledWith({ | ||
hsName: homeserver, | ||
hsUrl: wkHsUrl, | ||
hsNameIsDifferent: true, | ||
warning: null, | ||
isDefault: false, | ||
isNameResolvable: true, | ||
isUrl: wkIsUrl, | ||
}); | ||
|
||
await flushPromises(); | ||
}); | ||
|
||
it("should fall back to static config when well-known lookup fails", async () => { | ||
const homeserver = "myhomeserver3.site"; | ||
// this server returns 404 for well-known | ||
const wellKnownUrl = `https://${homeserver}/.well-known/matrix/client`; | ||
fetchMock.get(wellKnownUrl, { status: 404 }); | ||
// but is otherwise live (happy versions response) | ||
fetchMock.get(`https://${homeserver}/_matrix/client/versions`, { versions: ["1"] }); | ||
const onFinished = jest.fn(); | ||
getComponent({ onFinished }); | ||
|
||
fireEvent.change(getOtherHomeserverInput(), { target: { value: homeserver } }); | ||
fireEvent.click(screen.getByText("Continue")); | ||
|
||
// validation on submit is async | ||
await flushPromises(); | ||
|
||
expect(fetchMock).toHaveFetched(wellKnownUrl); | ||
expect(fetchMock).toHaveFetched(`https://${homeserver}/_matrix/client/versions`); | ||
|
||
expect(onFinished).toHaveBeenCalledWith({ | ||
hsName: homeserver, | ||
hsUrl: "https://" + homeserver, | ||
hsNameIsDifferent: false, | ||
warning: null, | ||
isDefault: false, | ||
isNameResolvable: false, | ||
isUrl: defaultServerConfig.isUrl, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is right, this is likely to clobber the reading of any child text like
defaultServerName
, why is this change in a PR titled unit testing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I wanted an easy way to select this for testing and the aria-label seemed harmless. Changed it to a testid.