-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f90142
commit e22fd43
Showing
23 changed files
with
518 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"live-mobile": patch | ||
--- | ||
|
||
Add Spam Filtering for NFTs in TX history |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
apps/ledger-live-mobile/src/hooks/nfts/__tests__/useHideSpamCollection.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useHideSpamCollection } from "../useHideSpamCollection"; | ||
import { useDispatch } from "react-redux"; | ||
import { renderHook } from "@tests/test-renderer"; | ||
import { hideNftCollection } from "~/actions/settings"; | ||
import { State } from "~/reducers/types"; | ||
|
||
jest.mock("react-redux", () => ({ | ||
...jest.requireActual("react-redux"), | ||
useDispatch: jest.fn(), | ||
})); | ||
|
||
const mockDispatch = jest.fn(); | ||
|
||
describe("useHideSpamCollection", () => { | ||
beforeEach(() => { | ||
(useDispatch as jest.Mock).mockReturnValue(mockDispatch); | ||
mockDispatch.mockClear(); | ||
}); | ||
|
||
it("should dispatch hideNftCollection action if collection is not whitelisted", () => { | ||
const { result } = renderHook( | ||
() => useHideSpamCollection(), | ||
|
||
{ | ||
overrideInitialState: (state: State) => ({ | ||
...state, | ||
settings: { | ||
...state.settings, | ||
whitelistedNftCollections: ["collectionA", "collectionB"], | ||
hiddenNftCollections: [], | ||
}, | ||
}), | ||
}, | ||
); | ||
result.current.hideSpamCollection("collectionC"); | ||
|
||
expect(mockDispatch).toHaveBeenCalledWith(hideNftCollection("collectionC")); | ||
}); | ||
|
||
it("should not dispatch hideNftCollection action if collection is whitelisted", () => { | ||
const { result } = renderHook(() => useHideSpamCollection(), { | ||
overrideInitialState: (state: State) => ({ | ||
...state, | ||
settings: { | ||
...state.settings, | ||
hiddenNftCollections: [], | ||
whitelistedNftCollections: ["collectionA", "collectionB"], | ||
}, | ||
}), | ||
}); | ||
result.current.hideSpamCollection("collectionA"); | ||
|
||
expect(mockDispatch).not.toHaveBeenCalled(); | ||
}); | ||
}); |
90 changes: 90 additions & 0 deletions
90
apps/ledger-live-mobile/src/hooks/nfts/__tests__/useSyncNFTsWitHAccount.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { useSelector } from "react-redux"; | ||
import { useFeature } from "@ledgerhq/live-common/featureFlags/index"; | ||
import { useCheckNftAccount } from "@ledgerhq/live-nft-react"; | ||
import { useHideSpamCollection } from "../useHideSpamCollection"; | ||
import { useSyncNFTsWithAccounts } from "../useSyncNFTsWithAccounts"; | ||
|
||
import { accountsSelector, orderedVisibleNftsSelector } from "~/reducers/accounts"; | ||
import { renderHook } from "@testing-library/react-native"; | ||
|
||
jest.mock("react-redux", () => ({ | ||
useSelector: jest.fn(), | ||
})); | ||
|
||
jest.mock("@ledgerhq/live-common/featureFlags/index", () => ({ | ||
useFeature: jest.fn(), | ||
})); | ||
|
||
jest.mock("../useHideSpamCollection", () => ({ | ||
useHideSpamCollection: jest.fn(), | ||
})); | ||
|
||
jest.mock("@ledgerhq/live-nft-react", () => ({ | ||
useCheckNftAccount: jest.fn(), | ||
isThresholdValid: jest.fn(), | ||
getThreshold: jest.fn(), | ||
})); | ||
|
||
describe("useSyncNFTsWithAccounts", () => { | ||
const mockUseSelector = useSelector as jest.Mock; | ||
const mockUseFeature = useFeature as jest.Mock; | ||
const mockUseHideSpamCollection = useHideSpamCollection as jest.Mock; | ||
const mockUseCheckNftAccount = useCheckNftAccount as jest.Mock; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it("should refetch periodically based on TIMER", () => { | ||
const mockRefetch = jest.fn(); | ||
const mockAccounts = [{ freshAddress: "0x123" }, { freshAddress: "0x456" }]; | ||
|
||
mockUseFeature.mockReturnValue({ enabled: true }); | ||
mockUseHideSpamCollection.mockReturnValue({ enabled: true, hideSpamCollection: jest.fn() }); | ||
mockUseSelector.mockImplementation(selector => { | ||
if (selector === accountsSelector) return mockAccounts; | ||
if (selector === orderedVisibleNftsSelector) return []; | ||
return []; | ||
}); | ||
mockUseCheckNftAccount.mockReturnValue({ refetch: mockRefetch }); | ||
|
||
renderHook(() => useSyncNFTsWithAccounts()); | ||
|
||
jest.advanceTimersByTime(5 * 60 * 60 * 1000); | ||
|
||
expect(mockRefetch).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should refetch immediately when a new account is added", () => { | ||
const mockRefetch = jest.fn(); | ||
const initialAccounts = [{ freshAddress: "0x123" }]; | ||
const updatedAccounts = [...initialAccounts, { freshAddress: "0x789" }]; | ||
|
||
mockUseFeature.mockReturnValue({ enabled: true }); | ||
mockUseHideSpamCollection.mockReturnValue({ enabled: true, hideSpamCollection: jest.fn() }); | ||
mockUseSelector | ||
.mockImplementationOnce(selector => { | ||
if (selector === accountsSelector) return initialAccounts; | ||
if (selector === orderedVisibleNftsSelector) return []; | ||
return []; | ||
}) | ||
.mockImplementationOnce(selector => { | ||
if (selector === accountsSelector) return updatedAccounts; | ||
if (selector === orderedVisibleNftsSelector) return []; | ||
return []; | ||
}); | ||
|
||
mockUseCheckNftAccount.mockReturnValue({ refetch: mockRefetch }); | ||
|
||
const { rerender } = renderHook(() => useSyncNFTsWithAccounts()); | ||
|
||
rerender({}); | ||
|
||
expect(mockRefetch).toHaveBeenCalledTimes(2); // 1 for initial render & 1 for adding new account | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
apps/ledger-live-mobile/src/hooks/nfts/useHideSpamCollection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useCallback } from "react"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { useFeature } from "@ledgerhq/live-common/featureFlags/index"; | ||
import { hideNftCollection } from "~/actions/settings"; | ||
import { whitelistedNftCollectionsSelector } from "~/reducers/settings"; | ||
|
||
export function useHideSpamCollection() { | ||
const spamFilteringTxFeature = useFeature("spamFilteringTx"); | ||
const whitelistedNftCollections = useSelector(whitelistedNftCollectionsSelector); | ||
|
||
const dispatch = useDispatch(); | ||
const hideSpamCollection = useCallback( | ||
(collection: string) => { | ||
if (!whitelistedNftCollections.includes(collection)) { | ||
dispatch(hideNftCollection(collection)); | ||
} | ||
}, | ||
[dispatch, whitelistedNftCollections], | ||
); | ||
|
||
return { | ||
hideSpamCollection, | ||
enabled: spamFilteringTxFeature?.enabled, | ||
}; | ||
} |
Oops, something went wrong.