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

Make accountslice take an array #289

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/components/AccountToContactTile.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("AccountOrContactTile", () => {
it("displays account label if the address is in accounts", () => {
const account = mockImplicitAccount(0);
const pkh = account.address.pkh;
store.dispatch(add(account));
store.dispatch(add([account]));
render(AccountOrContactTileFixture(pkh));
expect(screen.queryByTestId("account-or-contact-tile")).toHaveTextContent(account.label);
});
Expand Down
4 changes: 2 additions & 2 deletions src/utils/accountsReducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("Accounts reducer", () => {
});

test("should handle adding accounts and arrays of accounts", () => {
store.dispatch(add(mockImplicitAccount(1)));
store.dispatch(add([mockImplicitAccount(1)]));
expect(store.getState().accounts).toEqual({
items: [mockImplicitAccount(1)],
seedPhrases: {},
Expand All @@ -51,7 +51,7 @@ describe("Accounts reducer", () => {
test("adding account should throw and exception if it is a pkh duplicate and not modify state", () => {
store.dispatch(add([mockImplicitAccount(1), mockImplicitAccount(2), mockImplicitAccount(3)]));

expect(() => store.dispatch(add(mockImplicitAccount(2)))).toThrowError(
expect(() => store.dispatch(add([mockImplicitAccount(2)]))).toThrowError(
`Can't add account ${mockImplicitAccount(2).address.pkh} in store since it already exists.`
);

Expand Down
2 changes: 1 addition & 1 deletion src/utils/contactsReducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("Contacts reducer", () => {

test("should not add contact containing Account info", () => {
const account = mockImplicitAccount(0);
store.dispatch(add(account));
store.dispatch(add([account]));
store.dispatch(
checkAccountsAndUpsertContact({ name: account.label, pkh: account.address.pkh })
);
Expand Down
6 changes: 2 additions & 4 deletions src/utils/store/accountsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ const accountsSlice = createSlice({
state.items = newAccounts;
delete state.seedPhrases[fingerPrint];
},
add: (state, { payload }: { type: string; payload: ImplicitAccount | ImplicitAccount[] }) => {
const accounts = Array.isArray(payload) ? payload : [payload];
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure if passing in just one object was a problem but I'd write it as state.items = concatUnique(state.items, [payload].flat()); WDYT?

Copy link
Contributor Author

@ryutamago ryutamago Jul 24, 2023

Choose a reason for hiding this comment

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

i noticed there was no other places in the codebase that passes in a single item other than the tests so i think it's better to keep the API simple like this


state.items = concatUnique(state.items, accounts);
add: (state, { payload }: { type: string; payload: ImplicitAccount[] }) => {
state.items = concatUnique(state.items, payload);
},
},
});
Expand Down
32 changes: 18 additions & 14 deletions src/views/home/AccountList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,27 @@ const restore = async () => {
);

store.dispatch(
addAccounts({
type: AccountType.SOCIAL,
idp: "google",
address: mockImplicitAddress(6),
pk: mockPk(6),
label: GOOGLE_ACCOUNT_LABEL1,
})
addAccounts([
{
type: AccountType.SOCIAL,
idp: "google",
address: mockImplicitAddress(6),
pk: mockPk(6),
label: GOOGLE_ACCOUNT_LABEL1,
},
])
);

store.dispatch(
addAccounts({
type: AccountType.SOCIAL,
idp: "google",
address: mockImplicitAddress(7),
pk: mockPk(7),
label: GOOGLE_ACCOUNT_LABEL2,
})
addAccounts([
{
type: AccountType.SOCIAL,
idp: "google",
address: mockImplicitAddress(7),
pk: mockPk(7),
label: GOOGLE_ACCOUNT_LABEL2,
},
])
);

store.dispatch(
Expand Down