Skip to content

Commit

Permalink
refactor: use state
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed Jun 6, 2024
1 parent ff317cc commit 814e711
Show file tree
Hide file tree
Showing 7 changed files with 464 additions and 819 deletions.
6 changes: 6 additions & 0 deletions src/__mocks__/state-mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type Account,
type AuthState,
type GitifyState,
type GitifyUser,
type SettingsState,
Theme,
Expand Down Expand Up @@ -72,3 +73,8 @@ export const mockSettings: SettingsState = {
showAccountHostname: false,
delayNotificationState: false,
};

export const mockState: GitifyState = {
auth: mockAuth,
settings: mockSettings,
};
33 changes: 18 additions & 15 deletions src/context/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ describe('context/App.tsx', () => {
const markRepoNotificationsMock = jest.fn();
const markRepoNotificationsDoneMock = jest.fn();

const mockDefaultState = {
auth: { accounts: [], enterpriseAccounts: [], token: null, user: null },
settings: mockSettings,
};

beforeEach(() => {
(useNotifications as jest.Mock).mockReturnValue({
fetchNotifications: fetchNotificationsMock,
Expand Down Expand Up @@ -131,13 +136,7 @@ describe('context/App.tsx', () => {

expect(markNotificationReadMock).toHaveBeenCalledTimes(1);
expect(markNotificationReadMock).toHaveBeenCalledWith(
{
accounts: [],
enterpriseAccounts: [],
token: null,
user: null,
},
mockSettings,
mockDefaultState,
'123-456',
'github.com',
);
Expand Down Expand Up @@ -165,8 +164,7 @@ describe('context/App.tsx', () => {

expect(markNotificationDoneMock).toHaveBeenCalledTimes(1);
expect(markNotificationDoneMock).toHaveBeenCalledWith(
{ accounts: [], enterpriseAccounts: [], token: null, user: null },
mockSettings,
mockDefaultState,
'123-456',
'github.com',
);
Expand Down Expand Up @@ -194,8 +192,7 @@ describe('context/App.tsx', () => {

expect(unsubscribeNotificationMock).toHaveBeenCalledTimes(1);
expect(unsubscribeNotificationMock).toHaveBeenCalledWith(
{ accounts: [], enterpriseAccounts: [], token: null, user: null },
mockSettings,
mockDefaultState,
'123-456',
'github.com',
);
Expand Down Expand Up @@ -228,8 +225,7 @@ describe('context/App.tsx', () => {

expect(markRepoNotificationsMock).toHaveBeenCalledTimes(1);
expect(markRepoNotificationsMock).toHaveBeenCalledWith(
{ accounts: [], enterpriseAccounts: [], token: null, user: null },
mockSettings,
mockDefaultState,
'gitify-app/notifications-test',
'github.com',
);
Expand Down Expand Up @@ -262,8 +258,15 @@ describe('context/App.tsx', () => {

expect(markRepoNotificationsDoneMock).toHaveBeenCalledTimes(1);
expect(markRepoNotificationsDoneMock).toHaveBeenCalledWith(
{ accounts: [], enterpriseAccounts: [], token: null, user: null },
mockSettings,
{
auth: {
accounts: [],
enterpriseAccounts: [],
token: null,
user: null,
},
settings: mockSettings,
},
'gitify-app/notifications-test',
'github.com',
);
Expand Down
16 changes: 8 additions & 8 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {

// biome-ignore lint/correctness/useExhaustiveDependencies: We only want fetchNotifications to be called for certain account or setting changes.
useEffect(() => {
fetchNotifications(auth, settings);
fetchNotifications({ auth, settings });
}, [
settings.participating,
settings.showBots,
Expand All @@ -122,7 +122,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
]);

useInterval(() => {
fetchNotifications(auth, settings);
fetchNotifications({ auth, settings });
}, Constants.FETCH_INTERVAL);

// biome-ignore lint/correctness/useExhaustiveDependencies: We need to update tray title when settings or notifications changes.
Expand Down Expand Up @@ -213,37 +213,37 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
}, []);

const fetchNotificationsWithAccounts = useCallback(
async () => await fetchNotifications(auth, settings),
async () => await fetchNotifications({ auth, settings }),
[auth, settings, notifications],
);

const markNotificationReadWithAccounts = useCallback(
async (id: string, hostname: string) =>
await markNotificationRead(auth, settings, id, hostname),
await markNotificationRead({ auth, settings }, id, hostname),
[auth, notifications],
);

const markNotificationDoneWithAccounts = useCallback(
async (id: string, hostname: string) =>
await markNotificationDone(auth, settings, id, hostname),
await markNotificationDone({ auth, settings }, id, hostname),
[auth, notifications],
);

const unsubscribeNotificationWithAccounts = useCallback(
async (id: string, hostname: string) =>
await unsubscribeNotification(auth, settings, id, hostname),
await unsubscribeNotification({ auth, settings }, id, hostname),
[auth, notifications],
);

const markRepoNotificationsWithAccounts = useCallback(
async (repoSlug: string, hostname: string) =>
await markRepoNotifications(auth, settings, repoSlug, hostname),
await markRepoNotifications({ auth, settings }, repoSlug, hostname),
[auth, notifications],
);

const markRepoNotificationsDoneWithAccounts = useCallback(
async (repoSlug: string, hostname: string) =>
await markRepoNotificationsDone(auth, settings, repoSlug, hostname),
await markRepoNotificationsDone({ auth, settings }, repoSlug, hostname),
[auth, notifications],
);

Expand Down
Loading

0 comments on commit 814e711

Please sign in to comment.