Skip to content

Commit

Permalink
fix(proposal): should show the proposal you submitted and not the pre…
Browse files Browse the repository at this point in the history
…vious (#942)

* add check that the submitted proposal has status DRAFT

which is failing because of issue After submitting a proposal draft, you see another proposal #936

* add check that for a proposal with the next id to show up, and that the status is DRAFT

* test for creating a proposal without dont ask again cache values set

* set cached proposal ids value to reproduce bug

* some additional delay on the view call so that it does not finish before opening the cache

* only change result for get_proposal if asking for the newly submitted proposal

* fix issue by checking that the latest proposal title and description matches what was submitted

* move locate of submit button closer to where it's used

* reduce flakyness by adding timeouts
  • Loading branch information
petersalomonsen authored Sep 29, 2024
1 parent 23d372d commit 2db2a47
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 24 deletions.
20 changes: 17 additions & 3 deletions instances/devhub.near/widget/devhub/entity/proposal/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,23 @@ useEffect(() => {
Array.isArray(proposalIdsArray) &&
proposalIds.length !== proposalIdsArray.length
) {
setCreateTxn(false);
setProposalId(proposalIds[proposalIds.length - 1]);
setShowProposalPage(true);
const latestProposalId = proposalIds[proposalIds.length - 1];
const latestProposal = Near.view(
"${REPL_DEVHUB_CONTRACT}",
"get_proposal",
{
proposal_id: latestProposalId,
}
);

if (
latestProposal.snapshot.name === title &&
latestProposal.snapshot.description === description
) {
setCreateTxn(false);
setProposalId(proposalIds[proposalIds.length - 1]);
setShowProposalPage(true);
}
}
}
}
Expand Down
129 changes: 110 additions & 19 deletions playwright-tests/tests/proposal/proposals.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { test as base, expect } from "@playwright/test";
import { pauseIfVideoRecording } from "../../testUtils.js";
import {
getCacheValue,
setCacheValue,
setCommitWritePermissionDontAskAgainCacheValues,
setDontAskAgainCacheValues,
} from "../../util/cache.js";
Expand Down Expand Up @@ -83,9 +85,19 @@ test.describe("Don't ask again enabled", () => {
});
test("should create a proposal", async ({ page, account }) => {
test.setTimeout(120000);

await page.goto(`/${account}/widget/app?page=proposals`);

const widgetSrc = `${account}/widget/devhub.entity.proposal.Editor`;
await setCacheValue({
page,
key: JSON.stringify({
action: "ViewCall",
contractId: account,
methodName: "get_all_proposal_ids",
}),
value: [0, 1, 2, 3, 4],
});

await setDontAskAgainCacheValues({
page,
Expand All @@ -96,19 +108,17 @@ test.describe("Don't ask again enabled", () => {

await page.getByRole("button", { name: " Submit Proposal" }).click();

const proposalTitle = "Test proposal 123456";
const proposalDescription = "The test proposal description.";
const titleArea = await page.getByRole("textbox").first();
await titleArea.fill("Test proposal 123456");
await titleArea.fill(proposalTitle);
await titleArea.blur();
await pauseIfVideoRecording(page);

const categoryDropdown = await page.locator(".dropdown-toggle").first();
await categoryDropdown.click();
await page.locator(".dropdown-menu > div > div:nth-child(2) > div").click();

const disabledSubmitButton = await page.locator(
".submit-draft-button.disabled"
);

const summary = await page.locator('textarea[type="text"]');
await summary.fill("Test proposal summary 123456789");
await summary.blur();
Expand All @@ -118,36 +128,110 @@ test.describe("Don't ask again enabled", () => {
.frameLocator("iframe")
.locator(".CodeMirror textarea");
await descriptionArea.focus();
await descriptionArea.fill("The test proposal description.");
await descriptionArea.fill(proposalDescription);
await descriptionArea.blur();
await pauseIfVideoRecording(page);

await page.locator('input[type="text"]').nth(2).fill("1000");
const proposalAmount = "1000";
await page.locator('input[type="text"]').nth(2).fill(proposalAmount);
await pauseIfVideoRecording(page);
await page.getByRole("checkbox").first().click();
await pauseIfVideoRecording(page);

const disabledSubmitButton = await page.locator(
".submit-draft-button.disabled"
);

await expect(disabledSubmitButton).toBeAttached();
await page.getByRole("checkbox").nth(1).click();
await pauseIfVideoRecording(page);
await expect(disabledSubmitButton).not.toBeAttached();

let newProposalId = 0;
await mockTransactionSubmitRPCResponses(
page,
async ({ route, request, transaction_completed, last_receiver_id }) => {
const postData = request.postDataJSON();
if (
transaction_completed &&
postData.params?.method_name === "get_all_proposal_ids"
) {
if (postData.params?.method_name === "get_all_proposal_ids") {
const response = await route.fetch();
const json = await response.json();

console.log(
"transaction completed, modifying get_proposal_ids result"
);
await new Promise((resolve) => setTimeout(() => resolve(), 4000));
const resultObj = decodeResultJSON(json.result.result);
resultObj.push(1);
console.log(JSON.stringify(resultObj));
newProposalId = resultObj[resultObj.length - 1] + 1;
if (transaction_completed) {
resultObj.push(newProposalId);
}
json.result.result = encodeResultJSON(resultObj);

await route.fulfill({ response, json });
} else if (
postData.params?.method_name === "get_proposal" &&
postData.params.args_base64 ===
btoa(JSON.stringify({ proposal_id: newProposalId }))
) {
postData.params.args_base64 = btoa(
JSON.stringify({ proposal_id: newProposalId - 1 })
);
const response = await route.fetch({
postData: JSON.stringify(postData),
});
const json = await response.json();

let resultObj = decodeResultJSON(json.result.result);

resultObj = {
proposal_version: "V0",
id: newProposalId,
author_id: "petersalomonsen.near",
social_db_post_block_height: "128860426",
snapshot: {
editor_id: "petersalomonsen.near",
timestamp: "1727265468109441208",
labels: [],
proposal_body_version: "V2",
name: proposalTitle,
category: "DevDAO Platform",
summary: "summary",
description: proposalDescription,
linked_proposals: [],
requested_sponsorship_usd_amount: proposalAmount,
requested_sponsorship_paid_in_currency: "USDT",
receiver_account: "petersalomonsen.near",
requested_sponsor: "neardevdao.near",
supervisor: "theori.near",
timeline: {
timeline_version: "V1",
status: "DRAFT",
sponsor_requested_review: false,
reviewer_completed_attestation: false,
kyc_verified: false,
},
linked_rfp: null,
},
snapshot_history: [
{
editor_id: "petersalomonsen.near",
timestamp: "1727265421865873611",
labels: [],
proposal_body_version: "V0",
name: proposalTitle,
category: "DevDAO Platform",
summary: "summary",
description: proposalDescription,
linked_proposals: [],
requested_sponsorship_usd_amount: proposalAmount,
requested_sponsorship_paid_in_currency: "USDT",
receiver_account: "petersalomonsen.near",
requested_sponsor: "neardevdao.near",
supervisor: "theori.near",
timeline: {
status: "DRAFT",
},
},
],
};

json.result.result = encodeResultJSON(resultObj);

await route.fulfill({ response, json });
Expand All @@ -161,6 +245,7 @@ test.describe("Don't ask again enabled", () => {
await submitButton.scrollIntoViewIfNeeded();
await submitButton.hover();
await pauseIfVideoRecording(page);

await submitButton.click();
await expect(disabledSubmitButton).toBeAttached();

Expand All @@ -182,6 +267,10 @@ test.describe("Don't ask again enabled", () => {
await expect(
await page.getByRole("button", { name: "Edit" })
).toBeVisible();

await expect(await page.getByText(`#${newProposalId}`)).toBeVisible();
await expect(await page.getByText("DRAFT", { exact: true })).toBeVisible();

await pauseIfVideoRecording(page);
});
});
Expand Down Expand Up @@ -533,7 +622,7 @@ test.describe("Wallet is connected", () => {

const delay_milliseconds_between_keypress_when_typing = 0;
const titleArea = await page.getByRole("textbox").first();
await expect(titleArea).toBeEditable();
await expect(titleArea).toBeEditable({ timeout: 10_000 });
await titleArea.pressSequentially("Test proposal 123456", {
delay: delay_milliseconds_between_keypress_when_typing,
});
Expand Down Expand Up @@ -674,7 +763,9 @@ test.describe("Wallet is connected", () => {
).toBeVisible();
const loader = page.getByRole("img", { name: "loader" });
expect(loader).toBeHidden({ timeout: 10000 });
await expect(page.getByText(`By ${profileName} ・`).first()).toBeVisible();
await expect(page.getByText(`By ${profileName} ・`).first()).toBeVisible({
timeout: 10_000,
});
});

test("should filter proposals by search text", async ({ page }) => {
Expand All @@ -687,7 +778,7 @@ test.describe("Wallet is connected", () => {
const loader = page.getByRole("img", { name: "loader" });
expect(loader).toBeHidden({ timeout: 10000 });
const element = page.locator(`:has-text("${term}")`).nth(1);
await expect(element).toBeVisible();
await expect(element).toBeVisible({ timeout: 10_000 });
});
});
});
4 changes: 2 additions & 2 deletions playwright-tests/util/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export async function setCacheValue({ page, key, value }) {
);
}

export async function getCacheValue(key) {
const storedData = await page.evaluate(async (key) => {
export async function getCacheValue(page, key) {
return await page.evaluate(async (key) => {
return await new Promise((resolve) => {
const dbName = "cacheDb";
const storeName = "cache-v1";
Expand Down

0 comments on commit 2db2a47

Please sign in to comment.