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

Refactor reducers to use RTK #80

Merged
merged 7 commits into from
Mar 10, 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
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"settings": { "react": { "version": "detect" } },
"rules": {
"eqeqeq": "error",
"no-param-reassign": ["error", { "props": true }],
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/no-unused-vars": [
"error",
Expand Down
58 changes: 53 additions & 5 deletions __tests__/lib/weechat/action_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { AnyAction, configureStore } from '@reduxjs/toolkit';
import { StoreState, reducer } from '../../../src/store';
import { transformToReduxAction } from '../../../src/lib/weechat/action_transformer';
import { ThunkAction } from 'redux-thunk';
import {
fetchBuffersAction,
fetchBuffersRemovedAction
} from '../../../src/store/actions';

describe('transformToReduxAction', () => {
describe('on buffers', () => {
Expand Down Expand Up @@ -68,12 +72,9 @@ describe('transformToReduxAction', () => {
);
expect(dispatch).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ type: 'FETCH_BUFFERS_REMOVED' })
);
expect(dispatch).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ type: 'FETCH_BUFFERS' })
fetchBuffersRemovedAction([])
);
expect(dispatch).toHaveBeenNthCalledWith(2, fetchBuffersAction({}));
});

it('preserves currentBufferId if the buffer is still open', () => {
Expand Down Expand Up @@ -212,4 +213,51 @@ describe('transformToReduxAction', () => {
});
});
});

describe('on _nicklist_diff', () => {
it('synchronizes the nicklist state with the provided diff', () => {
const preloadedState = {
nicklists: {
'84ed37600': [
{
name: 'oldnick',
pointers: ['84ed37600', '85139f000']
} as WeechatNicklist
]
}
};
const store = configureStore({ reducer, preloadedState });

const action = transformToReduxAction({
id: '_nicklist_diff',
header: { compression: 0, length: 0 },
objects: [
{
type: 'hda',
content: [
{
_diff: 45,
group: 0,
name: 'oldnick',
pointers: ['84ed37600', '85139f000']
},
{
_diff: 43,
group: 0,
name: 'newnick',
pointers: ['84ed37600', '85139f000']
}
]
}
]
});
expect(action).toBeDefined();

store.dispatch(action!);

expect(store.getState().nicklists).toMatchObject({
'84ed37600': [{ name: 'newnick', pointers: ['84ed37600', '85139f000'] }]
});
});
});
});
17 changes: 7 additions & 10 deletions __tests__/lib/weechat/connection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import WeechatConnection, {
ConnectionError
} from '../../../src/lib/weechat/connection';
import {
disconnectAction,
fetchVersionAction
} from '../../../src/store/actions';

const mockWebSocket = jest.fn(function () {
this.send = jest.fn();
Expand Down Expand Up @@ -86,10 +90,7 @@ describe(WeechatConnection, () => {

expect(onSuccess).toHaveBeenCalled();
expect(onError).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith({
type: 'FETCH_VERSION',
payload: '4.1.2'
});
expect(dispatch).toHaveBeenCalledWith(fetchVersionAction('4.1.2'));
});

describe('close', () => {
Expand Down Expand Up @@ -120,9 +121,7 @@ describe(WeechatConnection, () => {
'quit\n'
);
expect(mockWebSocket.mock.instances[0].close).toHaveBeenCalled();
expect(dispatch).toHaveBeenNthCalledWith(2, {
type: 'DISCONNECT'
});
expect(dispatch).toHaveBeenNthCalledWith(2, disconnectAction());
expect(mockWebSocket.mock.instances).toHaveLength(1);
});

Expand Down Expand Up @@ -151,9 +150,7 @@ describe(WeechatConnection, () => {
mockWebSocket.mock.instances[0].close();

expect(onError).toHaveBeenCalledWith(true, ConnectionError.Socket);
expect(dispatch).toHaveBeenNthCalledWith(2, {
type: 'DISCONNECT'
});
expect(dispatch).toHaveBeenNthCalledWith(2, disconnectAction());
expect(mockWebSocket.mock.instances).toHaveLength(2);
});
});
Expand Down
Loading