Skip to content

Commit

Permalink
fix: Make admin proposals page work.
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgcramos authored Aug 11, 2021
1 parent 223700d commit 8f3f5c0
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/hooks/api/useProposalsBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,15 @@ export default function useProposalsBatch({
});
setRemainingTokens(unfetchedTokens);
setStatusIndex(index);
if (!foundPreviousSessionStatus) {
setStatuses(statuses);
return send(RESOLVE);
}
// machine stop condition: inventory loaded, but no tokens to fetch
if (isByRecordStatus) {
proposalStatus = statuses;
} else {
setStatuses(statuses);
}
// machine stop condition: inventory loaded, but no tokens to fetch
if (!foundPreviousSessionStatus) {
return send(RESOLVE);
}
return send(START);
};

Expand All @@ -281,6 +280,7 @@ export default function useProposalsBatch({

const anyError = error || state.error;
useThrowError(anyError);

return {
proposals: getRfpLinkedProposals(
proposalWithCacheVotetatus(proposals),
Expand All @@ -290,7 +290,7 @@ export default function useProposalsBatch({
proposalsTokens: allByStatus,
loading:
state.loading ||
(!values(proposals).length && statusIndex + 1 < voteStatuses.length),
(!values(proposals).length && statusIndex + 1 < voteStatuses?.length),
// loading return true when fetching cycle is running and there are no proposals fetched to avoid flickering at starting
verifying: state.verifying,
onRestartMachine,
Expand Down
73 changes: 68 additions & 5 deletions teste2e/cypress/e2e/records/recordsList.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {shortRecordToken} from "../../utils";
import { shortRecordToken } from "../../utils";

const statusByTab = {
"Under Review": ["started", "authorized", "unauthorized"],
Approved: ["approved"],
Rejected: ["rejected"],
Abandoned: ["ineligible"]
Abandoned: ["ineligible"],
Unreviewed: ["unreviewed"],
Censored: ["censored"]
};

const RECORDS_PAGE_SIZE = 5;
Expand All @@ -18,7 +20,7 @@ const getTokensByStatusTab = (inventory, currentTab) =>
: [];

describe("Records list", () => {
describe("proposal list", () => {
describe("proposals list", () => {
beforeEach(() => {
cy.middleware("ticketvote.inventory", {
approved: 4,
Expand Down Expand Up @@ -70,7 +72,6 @@ describe("Records list", () => {
cy.wait(1000);
cy.assertListLengthByTestId("record-title", 28);
});

it("can switch tabs and load proposals correctly", () => {
cy.visit("/?tab=approved");
cy.wait("@ticketvote.inventory");
Expand Down Expand Up @@ -106,7 +107,6 @@ describe("Records list", () => {
cy.viewport(1001, 500);
cy.findByTestId("sidebar").should("be.visible");
});

it("can render loading placeholders properly", () => {
cy.visit(`/`);
cy.get('[data-testid="loading-placeholders"] > div').should(
Expand All @@ -115,4 +115,67 @@ describe("Records list", () => {
);
});
});

describe("admin proposals list", () => {
beforeEach(() => {
const admin = {
email: "[email protected]",
username: "adminuser",
password: "password"
};
cy.middleware("records.inventory", {
unreviewed: 22,
censored: 8
});
cy.middleware("records.records");
cy.login(admin);
});
it("can render records list according to inventory order", () => {
let inventory;
cy.visit("/admin/records");
cy.wait("@records.inventory").then(({ response: { body } }) => {
inventory = body.unvetted;
});
cy.wait("@records.records");
cy.assertListLengthByTestId("record-title", RECORDS_PAGE_SIZE) // first records batch
.each(([{ id }], position) => {
const tokens = getTokensByStatusTab(inventory, "Unreviewed");
const expectedToken = shortRecordToken(tokens[position]);
expect(id).to.have.string(expectedToken);
});
});
it("can render records and inventory pagination correctly", () => {
cy.visit("/admin/records");
cy.wait("@records.inventory");
cy.wait("@records.records");
cy.assertListLengthByTestId("record-title", 5);
cy.scrollTo("bottom");
cy.wait("@records.records");
cy.assertListLengthByTestId("record-title", 10);
cy.scrollTo("bottom");
cy.wait("@records.records");
cy.assertListLengthByTestId("record-title", 15);
cy.scrollTo("bottom");
cy.wait("@records.records");
cy.assertListLengthByTestId("record-title", 20);
cy.scrollTo("bottom");
cy.wait("@records.records");
cy.assertListLengthByTestId("record-title", 22);
cy.scrollTo("bottom");
cy.wait(1000);
cy.assertListLengthByTestId("record-title", 22);
});
it("can switch tabs and load proposals correctly", () => {
cy.visit("/admin/records?tab=unreviewed");
cy.wait("@records.inventory");
cy.wait("@records.records");
cy.assertListLengthByTestId("record-title", 5);
cy.findByTestId("tab-1").click();
cy.wait("@records.records");
cy.assertListLengthByTestId("record-title", 5);
cy.findByTestId("tab-0").click();
cy.wait("@records.records");
cy.assertListLengthByTestId("record-title", 10);
});
});
});
44 changes: 42 additions & 2 deletions teste2e/cypress/support/mock/records.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { makeProposal } from "../../utils";
import { buildRecord, buildProposal } from "../generate";

import {
buildRecord,
buildProposal,
makeCustomInventoryByStatus
} from "../generate";
import { getProposalStatusLabel, getProposalStateLabel } from "../../utils";
// Record
const DEFAULT_STATUS = 2;
const DEFAULT_STATE = 2;
Expand Down Expand Up @@ -57,5 +61,41 @@ export const middlewares = {
req.reply({
body: { records: proposals }
});
}),
inventory: (tokensAmountByStatus, state = 1) =>
cy.intercept("/api/records/v1/inventory", (req) => {
const inv = makeCustomInventoryByStatus(
tokensAmountByStatus || {
censored: 7,
unreviewed: 8
}
);
const recordState = getProposalStateLabel(state);
if (!req.body.status) {
const tokens = Object.keys(inv).reduce(
(acc, status) => ({
...acc,
[status]: inv[status][0]
}),
{}
);
req.reply({
body: {
bestBlock: 718066,
[recordState]: tokens
}
});
} else {
const statusLabel = getProposalStatusLabel(req.body.status, true);
req.reply({
body: {
bestBlock: 718066,
[recordState]: {
[statusLabel]:
inv[statusLabel][req.body.page ? req.body.page - 1 : 0]
}
}
});
}
})
};
8 changes: 8 additions & 0 deletions teste2e/cypress/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const INELIGIBLE = "ineligible";
const ARCHIVED = "archived";
const PUBLIC = "public";
const CENSORED = "censored";
const PROPOSAL_STATE_UNVETTED = 1;
const PROPOSAL_STATE_VETTED = 2;
const PROPOSAL_VOTING_NOT_AUTHORIZED = 1;
const PROPOSAL_VOTING_AUTHORIZED = 2;
const PROPOSAL_VOTING_ACTIVE = 3;
Expand Down Expand Up @@ -178,3 +180,9 @@ export const getProposalStatusLabel = (status, isByRecordStatus) =>
[PROPOSAL_VOTING_INELIGIBLE]: INELIGIBLE
}
);

export const getProposalStateLabel = (proposalState) =>
get(proposalState)({
[PROPOSAL_STATE_VETTED]: "vetted",
[PROPOSAL_STATE_UNVETTED]: "unvetted"
});

0 comments on commit 8f3f5c0

Please sign in to comment.