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

Add missing RouterLinkTester for RouterLink #1805

Open
joelpop opened this issue Jun 13, 2024 · 1 comment · May be fixed by #1810
Open

Add missing RouterLinkTester for RouterLink #1805

joelpop opened this issue Jun 13, 2024 · 1 comment · May be fixed by #1810
Assignees

Comments

@joelpop
Copy link
Collaborator

joelpop commented Jun 13, 2024

RouterLink is missing its "RouterLinkTester" ComponentTester.

Attempting to use AnchorTester in its place (analogous to using AnchorElement in e2e testing), such as

    test(find(Anchor.class)
            .withText("See previous visitors →")
            .single());

results in an error something like:

java.util.NoSuchElementException: /hello: No visible Anchor in HelloView[#hello-view, @style='justify-content:center;align-items:baseline', @theme='spacing'] matching Anchor and text='See previous visitors →': []. Component tree:
└── HelloView[#hello-view, @style='justify-content:center;align-items:baseline', @theme='spacing']
    └── HorizontalLayout[#hello-view, @style='justify-content:center;align-items:baseline', @theme='spacing']
        ├── TextField[label='Name', value='', placeholder='your name', clearButtonVisible='true']
        ├── Button[caption='Greet', @theme='primary']
        └── RouterLink[text='See previous visitors →']

(Using RouterLink.class in place of Anchor.class results in a compilation error.)

@joelpop
Copy link
Collaborator Author

joelpop commented Jun 24, 2024

This is probably all that is necessary.

/**
 *
 * Tester for RouterLink components.
 *
 * @param <T>
 *            component type
 */
@Tests(RouterLink.class)
public class RouterLinkTester<T extends RouterLink> extends ComponentTester<T> {

    /**
     * Wrap given component for testing.
     *
     * @param component target component
     */
    public RouterLinkTester(T component) {
        super(component);
    }

    /**
     * Gets the registered route class for the router-link.
     * Returns an empty optional if there is no corresponding navigation target.
     *
     * @return an {@link Optional} containing the navigation target class or empty if not found
     */
    public Optional<Class<? extends Component>> getRoute() {
        ensureComponentIsUsable();

        return RouteConfiguration.forSessionScope()
                .getRoute(getComponent().getHref());
    }

    /**
     * Click the router-link for navigation.
     *
     * @return navigated view
     */
    public Component click() {
        return getRoute()
                .map(navigationTarget -> UI.getCurrent().navigate(navigationTarget).orElseThrow(IllegalStateException::new))
                .orElseThrow(IllegalStateException::new);
    }
}

@joelpop joelpop self-assigned this Jul 1, 2024
@joelpop joelpop linked a pull request Jul 8, 2024 that will close this issue
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants