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

fix(examples,e2e): Add examples and tests for #1580 #1578

Merged
Merged
Show file tree
Hide file tree
Changes from 16 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
5 changes: 5 additions & 0 deletions examples/angular/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { SignUpWithEmailLambdaComponent } from 'src/pages/ui/components/authenti
import { SignUpWithPhoneComponent } from 'src/pages/ui/components/authenticator/sign-up-with-phone/sign-up-with-phone.component';
import { SignUpWithUsernameComponent } from 'src/pages/ui/components/authenticator/sign-up-with-username/sign-up-with-username.component';
import { UseAuthenticatorComponent } from 'src/pages/ui/components/authenticator/useAuthenticator/useAuthenticator.component';
import { UseAuthenticatorHomeComponent } from 'src/pages/ui/components/authenticator/useAuthenticator/home/useAuthenticatorHome.component';

const routes: Routes = [
{
Expand Down Expand Up @@ -101,6 +102,10 @@ const routes: Routes = [
path: 'ui/components/authenticator/useAuthenticator',
component: UseAuthenticatorComponent,
},
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

path: 'ui/components/authenticator/useAuthenticator/home',
component: UseAuthenticatorHomeComponent,
},
];

@NgModule({
Expand Down
2 changes: 2 additions & 0 deletions examples/angular/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { SignUpWithEmailLambdaComponent } from 'src/pages/ui/components/authenti
import { SignUpWithPhoneComponent } from 'src/pages/ui/components/authenticator/sign-up-with-phone/sign-up-with-phone.component';
import { SignUpWithUsernameComponent } from 'src/pages/ui/components/authenticator/sign-up-with-username/sign-up-with-username.component';
import { UseAuthenticatorComponent } from 'src/pages/ui/components/authenticator/useAuthenticator/useAuthenticator.component';
import { UseAuthenticatorHomeComponent } from 'src/pages/ui/components/authenticator/useAuthenticator/home/useAuthenticatorHome.component';

@NgModule({
declarations: [
Expand All @@ -49,6 +50,7 @@ import { UseAuthenticatorComponent } from 'src/pages/ui/components/authenticator
SignUpWithPhoneComponent,
SignUpWithUsernameComponent,
UseAuthenticatorComponent,
UseAuthenticatorHomeComponent,
],
imports: [
BrowserModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div>Hello, {{ authenticator.user?.username }}!</div>
<button (click)="handleClick($event)">Sign Out</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Component } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Amplify } from 'aws-amplify';
import { AuthenticatorService } from '@aws-amplify/ui-angular';

import awsExports from '../aws-exports';

@Component({
selector: 'use-authenticator',
templateUrl: 'useAuthenticatorHome.component.html',
})
export class UseAuthenticatorHomeComponent {
constructor(
public authenticator: AuthenticatorService,
private router: Router,
private route: ActivatedRoute
) {
Amplify.configure(awsExports);
}

public handleClick(event: Event) {
event.preventDefault();
this.authenticator.signOut();
this.router.navigate(['../'], { relativeTo: this.route });
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<ng-container *ngIf="authenticator.route === 'authenticated'">
<h2>Welcome, {{ authenticator.user.username }}!</h2>
<button (click)="authenticator.signOut()">Sign Out</button>
</ng-container>

<ng-container *ngIf="authenticator.route !== 'authenticated'">
<amplify-authenticator></amplify-authenticator>
</ng-container>
<amplify-authenticator>
<ng-template amplifySlot="authenticated">
<button (click)="navigateHome($event)">Navigate to Home</button>
</ng-template>
</amplify-authenticator>
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { Component } from '@angular/core';
import { Amplify } from 'aws-amplify';
import { Component, OnInit } from '@angular/core';
import { Amplify, Auth } from 'aws-amplify';
import { AuthenticatorService } from '@aws-amplify/ui-angular';

import awsExports from './aws-exports';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
selector: 'use-authenticator',
templateUrl: 'useAuthenticator.component.html',
})
export class UseAuthenticatorComponent {
constructor(public authenticator: AuthenticatorService) {
constructor(
public authenticator: AuthenticatorService,
private router: Router,
private route: ActivatedRoute
) {
Amplify.configure(awsExports);
}

public navigateHome(event: Event) {
event.preventDefault();
this.router.navigate(['home'], { relativeTo: this.route });
}
}
11 changes: 10 additions & 1 deletion examples/next/pages/_app.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
// https://nextjs.org/docs/advanced-features/custom-app
import App from 'next/app';
import { Amplify } from 'aws-amplify';
import { Authenticator, AmplifyProvider } from '@aws-amplify/ui-react';

if (typeof window !== 'undefined') {
window['Amplify'] = Amplify;
}

export default App;
export default function MyApp(props) {
return (
<AmplifyProvider>
<Authenticator.Provider>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was testing this in the other PR and I had to add this. Glad it's there now.

<App {...props} />
</Authenticator.Provider>
</AmplifyProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Amplify } from 'aws-amplify';
import { useAuthenticator } from '@aws-amplify/ui-react';
import * as React from 'react';
import awsExports from '../aws-exports';
import router from 'next/router';

Amplify.configure(awsExports);

export default function App() {
const { user, signOut } = useAuthenticator();

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
signOut();
router.push('../useAuthenticator');
};

return (
<>
<div>Hello, {user?.username}!</div>
<button onClick={handleClick}>Sign Out</button>
</>
);
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
import { Amplify } from 'aws-amplify';
import router from 'next/router';

import { Authenticator, useAuthenticator } from '@aws-amplify/ui-react';
import { Amplify } from 'aws-amplify';
import '@aws-amplify/ui-react/styles.css';

import awsExports from './aws-exports';
import { Authenticator } from '@aws-amplify/ui-react';

Amplify.configure(awsExports);

const Home = () => {
const { user, signOut } = useAuthenticator((context) => [context.user]);

return (
<>
<h2>Welcome, {user.username}!</h2>
<button onClick={signOut}>Sign Out</button>
</>
);
};

const Login = () => <Authenticator />;

function App() {
const { route } = useAuthenticator((context) => [context.route]);
return route === 'authenticated' ? <Home /> : <Login />;
}
export default function App() {
const navigateHome = () => router.push('useAuthenticator/home');

export default function AppWithProvider() {
return (
<Authenticator.Provider>
<App></App>
</Authenticator.Provider>
<Authenticator>
{() => <button onClick={navigateHome}>Navigate to Home</button>}
</Authenticator>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script setup lang="ts">
import { toRefs } from 'vue';
import { useRouter } from 'vue-router';

import { Amplify } from 'aws-amplify';
import { Authenticator, useAuthenticator } from '@aws-amplify/ui-vue';
import '@aws-amplify/ui-vue/styles.css';
import aws_exports from '../aws-exports';

Amplify.configure(aws_exports);

const router = useRouter();
const { user, signOut } = toRefs(useAuthenticator());
const handleClick = (event: Event) => {
event.preventDefault();
signOut.value();
router.push('../useAuthenticator');
};
</script>

<template>
<!-- TODO: this authenticator shouldn't be needed -->
<authenticator></authenticator>
Comment on lines +22 to +23
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vue still needed this hidden authenticator, because authenticator was needed to interpret and start the machine in the first place. This seemed like a much bigger refactor, so left this as-is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 I can look at this in a different PR. Maybe we need to add an initialization via provide/inject somewhere. A user could also hide this with CSS when the user is not logged in, I suppose

<div>Hello, {{ user?.username }}!</div>
<button @click="handleClick">Sign Out</button>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import aws_exports from './aws-exports';

Amplify.configure(aws_exports);

const { route, user, signOut } = toRefs(useAuthenticator());
const { route } = toRefs(useAuthenticator());
const props = useAuthenticator();
</script>

<template>
<authenticator> </authenticator>
<template v-if="route === 'authenticated'">
<h1>Hello {{ user?.username }}!</h1>
<button @click="signOut">Sign out</button>
</template>
<authenticator>
<template v-slot>
<router-link to="useAuthenticator/home">
<button>Navigate to Home</button>
</router-link>
</template>
</authenticator>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Then, When } from 'cypress-cucumber-preprocessor/steps';

Then('I see a valid greetings message', () => {
cy.findByRole('document')
.contains(new RegExp('^Hello, .+!$', 'i'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks if the message has "Hello, {username}" structure. Kept it generic to not expose the test username 😈

.should('exist');
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ Feature: Headless Usage
When I type my "username" with status "CONFIRMED"
And I type my password
And I click the "Sign in" button
Then I see "Sign out"
When I reload the page
Then I see "Sign out"
And I click the "Sign out" button
Then I see "Sign in"
Then I see "Navigate to Home"
And I click the "Navigate to Home" button
Then I see a valid greetings message
And I reload the page
Then I see a valid greetings message
And I click the "Sign Out" button
Then I see "Sign In"