From 7aa096d5742f958651baefc6f732516fc8046e37 Mon Sep 17 00:00:00 2001
From: Elliot Braem <16282460+elliotBraem@users.noreply.github.com>
Date: Mon, 1 Jul 2024 12:53:08 -0400
Subject: [PATCH 1/6] init
---
playwright-tests/code/auth/wallet.js | 16 +++++++
.../storage-states/wallet-connected.json | 23 +++++++++
.../storage-states/wallet-not-connected.json | 10 ++++
playwright-tests/testUtils.js | 27 +++++++++++
playwright-tests/tests/auth.spec.js | 48 +++++++++++++++++++
public/webcomponentapp.js | 6 ++-
src/App.js | 6 ++-
src/auth/Wallet.js | 14 ++++++
8 files changed, 148 insertions(+), 2 deletions(-)
create mode 100644 playwright-tests/code/auth/wallet.js
create mode 100644 playwright-tests/storage-states/wallet-connected.json
create mode 100644 playwright-tests/storage-states/wallet-not-connected.json
create mode 100644 playwright-tests/tests/auth.spec.js
create mode 100644 src/auth/Wallet.js
diff --git a/playwright-tests/code/auth/wallet.js b/playwright-tests/code/auth/wallet.js
new file mode 100644
index 0000000..e796faf
--- /dev/null
+++ b/playwright-tests/code/auth/wallet.js
@@ -0,0 +1,16 @@
+return (
+ <>
+
{context.accountId}
+ {
+ return context.accountId ? (
+
+ ) : (
+
+ );
+ }}
+ />
+ >
+);
diff --git a/playwright-tests/storage-states/wallet-connected.json b/playwright-tests/storage-states/wallet-connected.json
new file mode 100644
index 0000000..acf19af
--- /dev/null
+++ b/playwright-tests/storage-states/wallet-connected.json
@@ -0,0 +1,23 @@
+{
+ "cookies": [],
+ "origins": [
+ {
+ "origin": "http://localhost:3000",
+ "localStorage": [
+ {
+ "name": "near-wallet-selector:selectedWalletId",
+ "value": "\"my-near-wallet\""
+ },
+ {
+ "name": "near_app_wallet_auth_key",
+ "value": "{\"accountId\":\"anybody.near\"}"
+ },
+ {
+ "name": "near-social-vm:v01::accountId:",
+ "value": "anybody.near"
+ }
+ ]
+ }
+ ],
+ "sessionStorage": []
+}
\ No newline at end of file
diff --git a/playwright-tests/storage-states/wallet-not-connected.json b/playwright-tests/storage-states/wallet-not-connected.json
new file mode 100644
index 0000000..0b4ed34
--- /dev/null
+++ b/playwright-tests/storage-states/wallet-not-connected.json
@@ -0,0 +1,10 @@
+{
+ "cookies": [],
+ "origins": [
+ {
+ "origin": "http://localhost:3000",
+ "localStorage": []
+ }
+ ],
+ "sessionStorage": []
+}
\ No newline at end of file
diff --git a/playwright-tests/testUtils.js b/playwright-tests/testUtils.js
index 7c89430..09280dd 100644
--- a/playwright-tests/testUtils.js
+++ b/playwright-tests/testUtils.js
@@ -1,4 +1,6 @@
import { expect } from "@playwright/test";
+import path from "path";
+import fs from "fs";
export const pauseIfVideoRecording = async (page) => {
let isVideoRecorded = (await page.video()) ? true : false;
@@ -40,3 +42,28 @@ export const escapeHtml = (html) => {
.replace(/"/g, """)
.replace(/'/g, "'");
};
+
+export const useCode = async (page, filePath, props) => {
+ const fullPath = path.join(__dirname, "code", filePath);
+ try {
+ const code = fs.readFileSync(fullPath, "utf8");
+ const initialProps = props ? JSON.stringify(props) : "";
+
+ // Set code and initialProps attribute
+ await page.evaluate(
+ ({ code, initialProps }) => {
+ const viewer = document.querySelector("near-social-viewer");
+ viewer.setAttribute("code", code);
+ viewer.setAttribute("initialprops", initialProps);
+ },
+ { code, initialProps }
+ );
+
+ // Verify the viewer is visible
+ await waitForSelectorToBeVisible(page, "near-social-viewer");
+
+ await pauseIfVideoRecording(page);
+ } catch (err) {
+ throw new Error(`Error loading file: ${err.message}`);
+ }
+};
\ No newline at end of file
diff --git a/playwright-tests/tests/auth.spec.js b/playwright-tests/tests/auth.spec.js
new file mode 100644
index 0000000..4d44863
--- /dev/null
+++ b/playwright-tests/tests/auth.spec.js
@@ -0,0 +1,48 @@
+import { expect, test, describe } from "@playwright/test";
+
+describe("auth", () => {
+ test.beforeEach(async ({ page }) => {
+ await page.goto("/");
+ });
+
+ describe("User is not logged in", () => {
+ test.use({
+ storageState: "playwright-tests/storage-states/wallet-not-connected.json",
+ });
+
+ test("'context.accountId' should be null and show login button", async ({ page }) => {
+ await useCode(page, "auth/wallet.js");
+
+ const accountId = await page.textContent('[data-testid="accountId"]');
+ expect(accountId).toBe("null");
+ });
+
+ test("should show wallet modal after clicking login button", async ({ page }) => {
+ await useCode(page, "auth/wallet.js");
+
+ await page.click("#open-walletselector-button");
+
+ const modal = await page.waitForSelector("near-wallet-selector");
+ expect(modal).not.toBeNull();
+ });
+ });
+
+ describe("User is logged in", () => {
+ test.use({
+ storageState: "playwright-tests/storage-states/wallet-connected.json",
+ });
+
+ test("should have 'context.accountId' be non null and show logout button", async ({ page }) => {
+ await useCode(page, "auth/wallet.js");
+
+ const accountId = await page.textContent('[data-testid="accountId"]');
+ expect(accountId).toBe("anybody.near");
+ });
+
+ test("should prompt to disconnect wallet after clicking logout button", async ({ page }) => {
+ await useCode(page, "auth/wallet.js");
+
+ await page.getByRole("button", { name: "Log out" }).click();
+ });
+ });
+});
\ No newline at end of file
diff --git a/public/webcomponentapp.js b/public/webcomponentapp.js
index b7b4cb3..823373c 100644
--- a/public/webcomponentapp.js
+++ b/public/webcomponentapp.js
@@ -14,7 +14,11 @@ const modal = setupModal(selector, {
contractId: "social.near",
});
-document.getElementById('open-walletselector-button').addEventListener('click', () => modal.show());
+document.body.addEventListener('click', (event) => {
+ if (event.target && event.target.id === 'open-walletselector-button') {
+ modal.show();
+ }
+});
const viewer = document.querySelector('near-social-viewer');
viewer.selector = selector;
\ No newline at end of file
diff --git a/src/App.js b/src/App.js
index ecacc2b..4a96ce3 100644
--- a/src/App.js
+++ b/src/App.js
@@ -13,8 +13,9 @@ import {
RouterProvider,
useLocation,
} from "react-router-dom";
+import Wallet from "./auth/Wallet";
-const SESSION_STORAGE_REDIRECT_MAP_KEY = 'nearSocialVMredirectMap';
+const SESSION_STORAGE_REDIRECT_MAP_KEY = "nearSocialVMredirectMap";
function Viewer({ widgetSrc, code, initialProps }) {
const location = useLocation();
@@ -85,6 +86,9 @@ function App(props) {
}
return ;
},
+ Wallet: (props) => {
+ return ;
+ },
},
features: {
enableComponentSrcDataKey: true,
diff --git a/src/auth/Wallet.js b/src/auth/Wallet.js
new file mode 100644
index 0000000..f7f95c4
--- /dev/null
+++ b/src/auth/Wallet.js
@@ -0,0 +1,14 @@
+import { useNear } from "near-social-vm";
+import { useCallback } from "react";
+
+function Wallet({ provides }) {
+ const near = useNear();
+ const logOut = useCallback(async () => {
+ const wallet = await (await near.selector).wallet();
+ wallet.signOut();
+ }, [near]);
+
+ return provides({ logOut });
+}
+
+export default Wallet;
From 49be94833a7bad14f3df82eaea73f964d155c62b Mon Sep 17 00:00:00 2001
From: Elliot Braem <16282460+elliotBraem@users.noreply.github.com>
Date: Mon, 1 Jul 2024 15:23:57 -0400
Subject: [PATCH 2/6] tests
---
playwright-tests/code/auth/wallet.js | 4 +-
.../storage-states/wallet-connected.json | 12 +++-
playwright-tests/tests/auth.spec.js | 56 +++++++++++++++----
src/auth/Wallet.js | 4 +-
4 files changed, 58 insertions(+), 18 deletions(-)
diff --git a/playwright-tests/code/auth/wallet.js b/playwright-tests/code/auth/wallet.js
index e796faf..0d88b9d 100644
--- a/playwright-tests/code/auth/wallet.js
+++ b/playwright-tests/code/auth/wallet.js
@@ -2,9 +2,9 @@ return (
<>
{context.accountId}
{
+ provides={({ signOut }) => {
return context.accountId ? (
-
+
) : (