Skip to content

Commit

Permalink
Add end-to-end test app (#229)
Browse files Browse the repository at this point in the history
This adds a simple end-to-end test app allowing the user to log in, connect a websocket and see the events stream. The app should be linked to the local code so that the built current library is under tests.
  • Loading branch information
NSeydoux authored Apr 5, 2022
1 parent 0364585 commit c03e500
Show file tree
Hide file tree
Showing 21 changed files with 6,024 additions and 28 deletions.
28 changes: 0 additions & 28 deletions e2e/browser/.env.example

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions e2e/browser/testApp/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
35 changes: 35 additions & 0 deletions e2e/browser/testApp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
34 changes: 34 additions & 0 deletions e2e/browser/testApp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
91 changes: 91 additions & 0 deletions e2e/browser/testApp/components/appContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React, { useState, useEffect } from "react";
import {
login,
logout,
handleIncomingRedirect,
getDefaultSession,
Session,
ISessionInfo,
} from "@inrupt/solid-client-authn-browser";
import Notifications from "../notifications";

const REDIRECT_URL = window.location.href;
const APP_NAME = "Notifications browser-based tests app";
const DEFAULT_ISSUER = "https://login.inrupt.com/";

const NotificationContainer = ({
sessionInfo,
}: {
sessionInfo?: ISessionInfo;
}) => {
if (sessionInfo?.isLoggedIn) {
return <Notifications />;
} else {
return <></>;
}
};

export default function AppContainer() {
const [sessionInfo, setSessionInfo] = useState<ISessionInfo>();
const [issuer, setIssuer] = useState<string>(DEFAULT_ISSUER);

useEffect(() => {
handleIncomingRedirect().then(setSessionInfo);
}, []);

const handleLogin = async () => {
try {
// Login will redirect the user away so that they can log in the OIDC issuer,
// and back to the provided redirect URL (which should be controlled by your app).
await login({
redirectUrl: REDIRECT_URL,
oidcIssuer: issuer,
clientName: APP_NAME,
});
} catch (err) {
console.error(err);
}
};

const handleLogout = async () => {
await logout();
setSessionInfo(undefined);
};

return (
<div>
<h1>{APP_NAME}</h1>
<p>
{sessionInfo?.isLoggedIn
? `Logged in as ${sessionInfo.webId}`
: "Not logged in yet"}
</p>
<form>
<input
type="text"
value={issuer}
onChange={(e) => {
setIssuer(e.target.value);
}}
/>
<button
onClick={async (e) => {
e.preventDefault();
await handleLogin();
}}
>
Log In
</button>
<button
onClick={async (e) => {
e.preventDefault();
await handleLogout();
}}
>
Log Out
</button>
</form>
<NotificationContainer sessionInfo={sessionInfo} />
</div>
);
}
148 changes: 148 additions & 0 deletions e2e/browser/testApp/components/notifications/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { WebsocketNotification } from "@inrupt/solid-client-notifications";
import { useEffect, useState } from "react";
import {
createContainerInContainer,
getSourceIri,
deleteContainer,
getPodUrlAll,
} from "@inrupt/solid-client";
import { getDefaultSession } from "@inrupt/solid-client-authn-browser";

const session = getDefaultSession();

const MessageList = (props: { messages: Array<any> }) => {
const { messages } = props;
return (
<ul data-testid="eventList">
{messages.map((message) => (
<li key={message.id}>
<pre>{JSON.stringify(message, null, 2)}</pre>
</li>
))}
</ul>
);
};

export default function Notifications() {
const [socket, setSocket] = useState<WebsocketNotification>();
const [connectionStatus, setConnectionStatus] = useState<string>(
"disconnected"
);
const [parentContainerUrl, setParentContainerUrl] = useState<string>();
const [childContainerUrl, setChildContainerUrl] = useState<string>();
const [messageBus, setMessageBus] = useState<any[]>([]);

useEffect(() => {
if (session.info.webId !== undefined) {
getPodUrlAll(session.info.webId as string, {
fetch: session.fetch,
}).then((pods) => {
if (pods.length === 0) {
throw new Error("No pod root in webid profile");
}
setParentContainerUrl(pods[0]);
});
}
}, []);

useEffect(() => {
if (parentContainerUrl !== undefined && socket === undefined) {
setSocket(
new WebsocketNotification(parentContainerUrl, {
fetch: session.fetch,
gateway: "https://notification.inrupt.com",
})
);
}
if (socket !== undefined) {
socket.on("connected", () => setConnectionStatus("connected"));
socket.on("closed", () => {
setConnectionStatus("closed");
setMessageBus([]);
});
socket.on("error", () => setConnectionStatus("error"));
socket.on("message", (message) => {
setMessageBus((previousMessageBus) => [
JSON.parse(message),
...previousMessageBus,
]);
});
}
}, [socket, parentContainerUrl]);

return (
<div>
<p>
Websocket status:{" "}
<em>
<span data-testid="webSocketStatus">{connectionStatus}</span>
</em>
</p>
<p>
Child container:{" "}
<em>
<span data-testid="childContainerUrl">
{childContainerUrl ?? "None"}
</span>
</em>
</p>

<button
onClick={async (e) => {
e.preventDefault();
if (socket !== undefined) {
await socket.connect();
}
}}
data-testid="connectSocket"
>
Connect websocket
</button>
<button
onClick={(e) => {
e.preventDefault();
if (socket !== undefined) {
socket.disconnect();
}
}}
data-testid="disconnectSocket"
>
Disconnect websocket
</button>
<br></br>
<button
onClick={async (e) => {
e.preventDefault();
if (parentContainerUrl !== undefined) {
setChildContainerUrl(
getSourceIri(
await createContainerInContainer(parentContainerUrl, {
fetch: session.fetch,
})
)
);
}
}}
data-testid="createContainer"
>
Create container
</button>
<button
onClick={async (e) => {
e.preventDefault();
if (childContainerUrl !== undefined) {
deleteContainer(childContainerUrl, {
fetch: session.fetch,
});
setChildContainerUrl("None");
}
}}
data-testid="deleteContainer"
>
Delete container
</button>
<br />
<MessageList messages={messageBus} />
</div>
);
}
5 changes: 5 additions & 0 deletions e2e/browser/testApp/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
6 changes: 6 additions & 0 deletions e2e/browser/testApp/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
};

module.exports = nextConfig;
Loading

0 comments on commit c03e500

Please sign in to comment.