Skip to content

Commit 61e1ac7

Browse files
committed
small tweaks
Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent e5c5d03 commit 61e1ac7

2 files changed

Lines changed: 118 additions & 53 deletions

File tree

Lines changed: 91 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { fireEvent, render, screen } from '@testing-library/react';
1+
import { render, screen } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
23

34
import { mockSettings } from '../../__mocks__/state-mocks';
45
import { AppContext } from '../../context/App';
6+
import type { SettingsState } from '../../types';
57
import { OrganizationFilter } from './OrganizationFilter';
68

79
const mockUpdateFilter = jest.fn();
@@ -28,49 +30,101 @@ describe('components/filters/OrganizationFilter.tsx', () => {
2830
expect(screen.getByText('Exclude:')).toBeInTheDocument();
2931
});
3032

31-
it('should handle organization includes', () => {
32-
const props = {
33-
updateFilter: mockUpdateFilter,
34-
settings: mockSettings,
35-
};
33+
describe('Include organizations', () => {
34+
it('should handle organization includes', async () => {
35+
const props = {
36+
updateFilter: mockUpdateFilter,
37+
settings: mockSettings,
38+
};
3639

37-
render(
38-
<AppContext.Provider value={props}>
39-
<OrganizationFilter />
40-
</AppContext.Provider>,
41-
);
40+
render(
41+
<AppContext.Provider value={props}>
42+
<OrganizationFilter />
43+
</AppContext.Provider>,
44+
);
4245

43-
const includeInput = screen.getByTitle('Include organizations');
44-
fireEvent.change(includeInput, { target: { value: 'microsoft' } });
45-
fireEvent.blur(includeInput);
46+
await userEvent.type(
47+
screen.getByTitle('Include organizations'),
48+
'microsoft{enter}',
49+
);
4650

47-
expect(mockUpdateFilter).toHaveBeenCalledWith(
48-
'filterIncludeOrganizations',
49-
'microsoft',
50-
true,
51-
);
51+
expect(mockUpdateFilter).toHaveBeenCalledWith(
52+
'filterIncludeOrganizations',
53+
'microsoft',
54+
true,
55+
);
56+
});
57+
58+
it('should not allow duplicate include organizations', async () => {
59+
const props = {
60+
updateFilter: mockUpdateFilter,
61+
settings: {
62+
...mockSettings,
63+
filterIncludeOrganizations: ['microsoft'],
64+
} as SettingsState,
65+
};
66+
67+
render(
68+
<AppContext.Provider value={props}>
69+
<OrganizationFilter />
70+
</AppContext.Provider>,
71+
);
72+
73+
await userEvent.type(
74+
screen.getByTitle('Include organizations'),
75+
'microsoft{enter}',
76+
);
77+
78+
expect(mockUpdateFilter).toHaveBeenCalledTimes(0);
79+
});
5280
});
5381

54-
it('should handle organization excludes', () => {
55-
const props = {
56-
updateFilter: mockUpdateFilter,
57-
settings: mockSettings,
58-
};
82+
describe('Exclude organizations', () => {
83+
it('should handle organization excludes', async () => {
84+
const props = {
85+
updateFilter: mockUpdateFilter,
86+
settings: mockSettings,
87+
};
5988

60-
render(
61-
<AppContext.Provider value={props}>
62-
<OrganizationFilter />
63-
</AppContext.Provider>,
64-
);
89+
render(
90+
<AppContext.Provider value={props}>
91+
<OrganizationFilter />
92+
</AppContext.Provider>,
93+
);
6594

66-
const excludeInput = screen.getByTitle('Exclude organizations');
67-
fireEvent.change(excludeInput, { target: { value: 'github' } });
68-
fireEvent.blur(excludeInput);
95+
await userEvent.type(
96+
screen.getByTitle('Exclude organizations'),
97+
'github{enter}',
98+
);
6999

70-
expect(mockUpdateFilter).toHaveBeenCalledWith(
71-
'filterExcludeOrganizations',
72-
'github',
73-
true,
74-
);
100+
expect(mockUpdateFilter).toHaveBeenCalledWith(
101+
'filterExcludeOrganizations',
102+
'github',
103+
true,
104+
);
105+
});
106+
107+
it('should not allow duplicate exclude organizations', async () => {
108+
const props = {
109+
updateFilter: mockUpdateFilter,
110+
settings: {
111+
...mockSettings,
112+
filterExcludeOrganizations: ['github'],
113+
} as SettingsState,
114+
};
115+
116+
render(
117+
<AppContext.Provider value={props}>
118+
<OrganizationFilter />
119+
</AppContext.Provider>,
120+
);
121+
122+
await userEvent.type(
123+
screen.getByTitle('Exclude organizations'),
124+
'github{enter}',
125+
);
126+
127+
expect(mockUpdateFilter).toHaveBeenCalledTimes(0);
128+
});
75129
});
76130
});

src/renderer/utils/notifications/filters/filter.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,8 @@ export function filterBaseNotifications(
2424
return notifications.filter((notification) => {
2525
let passesFilters = true;
2626

27-
// Organization filters should always apply, regardless of detailed notifications setting
28-
if (hasIncludeOrganizationFilters(settings)) {
29-
passesFilters =
30-
passesFilters &&
31-
settings.filterIncludeOrganizations.some((organization) =>
32-
filterNotificationByOrganization(notification, organization),
33-
);
34-
}
35-
36-
if (hasExcludeOrganizationFilters(settings)) {
37-
passesFilters =
38-
passesFilters &&
39-
!settings.filterExcludeOrganizations.some((organization) =>
40-
filterNotificationByOrganization(notification, organization),
41-
);
42-
}
27+
passesFilters =
28+
passesFilters && passesOrganizationFilters(notification, settings);
4329

4430
if (subjectTypeFilter.hasFilters(settings)) {
4531
passesFilters =
@@ -126,6 +112,31 @@ function passesUserFilters(
126112
return passesFilters;
127113
}
128114

115+
function passesOrganizationFilters(
116+
notification: Notification,
117+
settings: SettingsState,
118+
): boolean {
119+
let passesFilters = true;
120+
121+
if (hasIncludeOrganizationFilters(settings)) {
122+
passesFilters =
123+
passesFilters &&
124+
settings.filterIncludeOrganizations.some((organization) =>
125+
filterNotificationByOrganization(notification, organization),
126+
);
127+
}
128+
129+
if (hasExcludeOrganizationFilters(settings)) {
130+
passesFilters =
131+
passesFilters &&
132+
!settings.filterExcludeOrganizations.some((organization) =>
133+
filterNotificationByOrganization(notification, organization),
134+
);
135+
}
136+
137+
return passesFilters;
138+
}
139+
129140
function passesStateFilter(
130141
notification: Notification,
131142
settings: SettingsState,

0 commit comments

Comments
 (0)