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

Extract RelayClient from RootView, other refactoring #101

Merged
merged 5 commits into from
Nov 22, 2024
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
22 changes: 12 additions & 10 deletions __tests__/lib/weechat/action_transformer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { UnknownAction, configureStore } from '@reduxjs/toolkit';
import { StoreState, reducer } from '../../../src/store';
import type { UnknownAction } from '@reduxjs/toolkit';
import { configureStore } from '@reduxjs/toolkit';
import type { ThunkAction } from 'redux-thunk';
import { transformToReduxAction } from '../../../src/lib/weechat/action_transformer';
import { ThunkAction } from 'redux-thunk';
import {
fetchBuffersAction,
fetchBuffersRemovedAction
} from '../../../src/store/actions';
import { AppState } from '../../../src/store/app';
import type { StoreState } from '../../../src/store';
import { reducer } from '../../../src/store';
import * as actions from '../../../src/store/actions';
import type { AppState } from '../../../src/store/app';

describe('transformToReduxAction', () => {
describe('on buffers', () => {
Expand Down Expand Up @@ -85,9 +84,12 @@ describe('transformToReduxAction', () => {
);
expect(dispatch).toHaveBeenNthCalledWith(
1,
fetchBuffersRemovedAction([])
actions.fetchBuffersRemovedAction([])
);
expect(dispatch).toHaveBeenNthCalledWith(
2,
actions.fetchBuffersAction({})
);
expect(dispatch).toHaveBeenNthCalledWith(2, fetchBuffersAction({}));
});

it('preserves currentBufferId if the buffer is still open', () => {
Expand Down
50 changes: 19 additions & 31 deletions __tests__/store/listeners.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { configureStore, createListenerMiddleware } from '@reduxjs/toolkit';
import { AppDispatch, reducer, StoreState } from '../../src/store';
import {
fetchBuffersAction,
pendingBufferNotificationAction,
pongAction
} from '../../src/store/actions';
import RelayClient from '../../src/lib/weechat/client';
import type { AppDispatch, StoreState } from '../../src/store';
import { reducer } from '../../src/store';
import * as actions from '../../src/store/actions';
import { PendingBufferNotificationListener } from '../../src/store/listeners';
import { RelayClient } from '../../src/usecase/Root';

jest.useFakeTimers();

describe(PendingBufferNotificationListener, () => {
it('ensures that the relay is connected before dispatching the notification', async () => {
const client = new (class implements RelayClient {
isConnected = jest.fn();
ping = jest.fn();
})();
const client = new RelayClient(jest.fn(), jest.fn(), jest.fn());
client.isConnected = jest.fn(() => true);
client.ping = jest.fn();

const listenerMiddleware = createListenerMiddleware();
const store = configureStore({
Expand Down Expand Up @@ -51,17 +47,15 @@ describe(PendingBufferNotificationListener, () => {
PendingBufferNotificationListener(client)
);

client.isConnected.mockReturnValue(true);

store.dispatch(
pendingBufferNotificationAction({
actions.pendingBufferNotificationAction({
identifier: '1fb4fc1d-530b-466f-85be-de27772de0a9',
bufferId: 1730555173010842,
lineId: 0
})
);

store.dispatch(pongAction());
store.dispatch(actions.pongAction());
await jest.runAllTimersAsync();

expect(client.isConnected).toHaveBeenCalled();
Expand All @@ -75,10 +69,9 @@ describe(PendingBufferNotificationListener, () => {
});

it('refreshes the buffer list when disconnected before dispatching the notification', async () => {
const client = new (class implements RelayClient {
isConnected = jest.fn();
ping = jest.fn();
})();
const client = new RelayClient(jest.fn(), jest.fn(), jest.fn());
client.isConnected = jest.fn(() => false);
client.ping = jest.fn();

const listenerMiddleware = createListenerMiddleware();
const store = configureStore({
Expand All @@ -92,18 +85,16 @@ describe(PendingBufferNotificationListener, () => {
PendingBufferNotificationListener(client)
);

client.isConnected.mockReturnValue(false);

store.dispatch(
pendingBufferNotificationAction({
actions.pendingBufferNotificationAction({
identifier: '1fb4fc1d-530b-466f-85be-de27772de0a9',
bufferId: 1730555173010842,
lineId: 0
})
);

store.dispatch(
fetchBuffersAction({
actions.fetchBuffersAction({
['83a41cd80']: {
_id: 1730555173010842,
full_name: 'irc.libera.#weechat',
Expand Down Expand Up @@ -136,10 +127,9 @@ describe(PendingBufferNotificationListener, () => {
});

it('does not dispatch the notification if the buffer is not in the buffer list', async () => {
const client = new (class implements RelayClient {
isConnected = jest.fn();
ping = jest.fn();
})();
const client = new RelayClient(jest.fn(), jest.fn(), jest.fn());
client.isConnected = jest.fn(() => false);
client.ping = jest.fn();

const listenerMiddleware = createListenerMiddleware();
const store = configureStore({
Expand All @@ -153,17 +143,15 @@ describe(PendingBufferNotificationListener, () => {
PendingBufferNotificationListener(client)
);

client.isConnected.mockReturnValue(false);

store.dispatch(
pendingBufferNotificationAction({
actions.pendingBufferNotificationAction({
identifier: '1fb4fc1d-530b-466f-85be-de27772de0a9',
bufferId: 1730555173010842,
lineId: 0
})
);

store.dispatch(fetchBuffersAction({}));
store.dispatch(actions.fetchBuffersAction({}));
await jest.runAllTimersAsync();

expect(client.isConnected).toHaveBeenCalled();
Expand Down
8 changes: 2 additions & 6 deletions __tests__/store/persist.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import {
Reducer,
configureStore,
createAction,
createReducer
} from '@reduxjs/toolkit';
import type { Reducer } from '@reduxjs/toolkit';
import { configureStore, createAction, createReducer } from '@reduxjs/toolkit';
import {
initializeStoreAction,
persistMiddleware,
Expand Down
39 changes: 13 additions & 26 deletions __tests__/usecase/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'react-native';
import App from '../../src/usecase/App';

import { act, render } from '../../src/test-utils';
import { configureStore } from '@reduxjs/toolkit';
import RelayClient from '../../src/lib/weechat/client';
import { reducer } from '../../src/store';
import { AppState } from '../../src/store/app';
import { bufferNotificationAction } from '../../src/store/actions';
import * as actions from '../../src/store/actions';
import type { AppState } from '../../src/store/app';
import { act, render } from '../../src/test-utils';

jest.mock('react-native-drawer-layout');

Expand Down Expand Up @@ -39,18 +40,11 @@ describe('App', () => {
app: { currentBufferId: bufferId } as AppState
}
});
const clearHotlistForBuffer = jest.fn();
const fetchBufferInfo = jest.fn();
const client = new RelayClient(jest.fn(), jest.fn(), jest.fn());
const fetchBufferInfo = (client.fetchBufferInfo = jest.fn());
const clearHotlistForBuffer = (client.clearHotlistForBuffer = jest.fn());

render(
<App
disconnect={() => {}}
fetchBufferInfo={fetchBufferInfo}
sendMessageToBuffer={() => {}}
clearHotlistForBuffer={clearHotlistForBuffer}
/>,
{ store }
);
render(<App disconnect={() => {}} client={client} />, { store });

expect(fetchBufferInfo).toHaveBeenCalledWith(bufferId);
expect(clearHotlistForBuffer).toHaveBeenCalledWith(bufferId);
Expand Down Expand Up @@ -89,22 +83,15 @@ describe('App', () => {
app: { currentBufferId: null } as AppState
}
});
const clearHotlistForBuffer = jest.fn();
const fetchBufferInfo = jest.fn();
const client = new RelayClient(jest.fn(), jest.fn(), jest.fn());
const fetchBufferInfo = (client.fetchBufferInfo = jest.fn());
const clearHotlistForBuffer = (client.clearHotlistForBuffer = jest.fn());

render(
<App
disconnect={() => {}}
fetchBufferInfo={fetchBufferInfo}
sendMessageToBuffer={() => {}}
clearHotlistForBuffer={clearHotlistForBuffer}
/>,
{ store }
);
render(<App disconnect={() => {}} client={client} />, { store });

act(() => {
store.dispatch(
bufferNotificationAction({
actions.bufferNotificationAction({
bufferId,
lineId: 0,
identifier: '1fb4fc1d-530b-466f-85be-de27772de0a9'
Expand Down
6 changes: 4 additions & 2 deletions __tests__/usecase/buffers/ui/Buffer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { fireEvent, render, screen } from '@testing-library/react-native';
import React from 'react';
import { ScrollView } from 'react-native';
import Buffer from '../../../../src/usecase/buffers/ui/Buffer';
import RelayClient from '../../../../src/lib/weechat/client';

describe(Buffer, () => {
const measureNickWidth = () => {
Expand Down Expand Up @@ -42,6 +43,7 @@ describe(Buffer, () => {
tags_array: ['irc_privmsg', 'notify_message']
} as WeechatLine
];
const client = new RelayClient(jest.fn(), jest.fn(), jest.fn());

render(
<Buffer
Expand All @@ -50,7 +52,7 @@ describe(Buffer, () => {
onLongPress={() => {}}
parseArgs={[]}
bufferId={'86c417600'}
fetchMoreLines={() => {}}
client={client}
clearNotification={() => {}}
/>
);
Expand All @@ -64,7 +66,7 @@ describe(Buffer, () => {
onLongPress={() => {}}
parseArgs={[]}
bufferId={'86c417600'}
fetchMoreLines={() => {}}
client={client}
clearNotification={() => {}}
notificationLineId={0}
/>
Expand Down
36 changes: 13 additions & 23 deletions __tests__/usecase/buffers/ui/BufferContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import BufferContainer from '../../../../src/usecase/buffers/ui/BufferContainer'
import { act, render, screen } from '../../../../src/test-utils';
import { reducer } from '../../../../src/store';
import { configureStore } from '@reduxjs/toolkit';
import {
bufferNotificationAction,
changeCurrentBufferAction,
fetchLinesAction
} from '../../../../src/store/actions';
import * as actions from '../../../../src/store/actions';
import RelayClient from '../../../../src/lib/weechat/client';

jest.mock('../../../../src/usecase/buffers/ui/Buffer');

Expand Down Expand Up @@ -46,14 +43,10 @@ describe('BufferContainer', () => {
enhancers: (getDefaultEnhancers) =>
getDefaultEnhancers({ autoBatch: false })
});
const client = new RelayClient(jest.fn(), jest.fn(), jest.fn());

render(
<BufferContainer
bufferId={bufferId}
showTopic={false}
sendMessage={() => {}}
fetchMoreLines={() => {}}
/>,
<BufferContainer bufferId={bufferId} showTopic={false} client={client} />,
{ store }
);

Expand All @@ -63,7 +56,7 @@ describe('BufferContainer', () => {

act(() => {
store.dispatch(
bufferNotificationAction({
actions.bufferNotificationAction({
bufferId,
lineId: 0,
identifier: '1fb4fc1d-530b-466f-85be-de27772de0a9'
Expand All @@ -74,14 +67,14 @@ describe('BufferContainer', () => {
expect(bufferContainer.props.notification).toBeNull();

act(() => {
store.dispatch(changeCurrentBufferAction(bufferId));
store.dispatch(actions.changeCurrentBufferAction(bufferId));
});

expect(bufferContainer.props.notification).toBeNull();

act(() => {
store.dispatch(
fetchLinesAction([
actions.fetchLinesAction([
{
id: 1730555173010842,
buffer: '86c417600',
Expand All @@ -107,6 +100,7 @@ describe('BufferContainer', () => {

it('clears notification after being handled by the buffer component', () => {
const ActualBuffer = jest.requireActual<
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
typeof import('../../../../src/usecase/buffers/ui/Buffer')
>('../../../../src/usecase/buffers/ui/Buffer').default;
jest.mocked(Buffer).mockImplementation((props) => {
Expand Down Expand Up @@ -142,20 +136,16 @@ describe('BufferContainer', () => {
enhancers: (getDefaultEnhancers) =>
getDefaultEnhancers({ autoBatch: false })
});
const client = new RelayClient(jest.fn(), jest.fn(), jest.fn());

render(
<BufferContainer
bufferId={bufferId}
showTopic={false}
sendMessage={() => {}}
fetchMoreLines={() => {}}
/>,
<BufferContainer bufferId={bufferId} showTopic={false} client={client} />,
{ store }
);

act(() => {
store.dispatch(
bufferNotificationAction({
actions.bufferNotificationAction({
bufferId,
lineId: 0,
identifier: '1fb4fc1d-530b-466f-85be-de27772de0a9'
Expand All @@ -166,12 +156,12 @@ describe('BufferContainer', () => {
expect(store.getState().app.notification).not.toBeNull();

act(() => {
store.dispatch(changeCurrentBufferAction(bufferId));
store.dispatch(actions.changeCurrentBufferAction(bufferId));
});

act(() => {
store.dispatch(
fetchLinesAction([
actions.fetchLinesAction([
{
id: 0,
buffer: '86c417600',
Expand Down
2 changes: 2 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default tseslint.config(
'no-param-reassign': ['error', { props: true }],

'@typescript-eslint/consistent-type-assertions': 'error',
'@typescript-eslint/consistent-type-exports': 'error',
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/no-deprecated': 'error',
'@typescript-eslint/no-misused-promises': [
'error',
Expand Down
4 changes: 2 additions & 2 deletions src/lib/helpers/parse-text-args.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import emojiRegex from 'emoji-regex';
import { TextStyle } from 'react-native';
import { ParseShape } from 'react-native-parsed-text';
import type { TextStyle } from 'react-native';
import type { ParseShape } from 'react-native-parsed-text';

export const getParseArgs = (
style: TextStyle,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers/push-notifications.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Constants from 'expo-constants';
import * as Device from 'expo-device';
import * as Notifications from 'expo-notifications';
import { EASConfig } from 'expo/config';
import type { EASConfig } from 'expo/config';

export const registerForPushNotificationsAsync = async (): Promise<void> => {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
Expand Down
Loading