Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 1f97da6

Browse files
committed
add more test for favourite button
1 parent 2e4a638 commit 1f97da6

File tree

4 files changed

+54
-69
lines changed

4 files changed

+54
-69
lines changed

src/components/views/messages/MessageActionBar.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ const ReplyInThreadButton = ({ mxEvent }: IReplyInThreadButton) => {
237237
</RovingAccessibleTooltipButton>;
238238
};
239239

240-
const FavouriteButton = ({ mxEvent }) => {
240+
interface IFavouriteButtonProp {
241+
mxEvent: MatrixEvent;
242+
}
243+
const FavouriteButton = ({ mxEvent }: IFavouriteButtonProp) => {
241244
const { favouriteMessageIds, dispatch } = useContext(FavouriteMessageContext);
242245
const eventId = mxEvent.getId();
243246

src/contexts/FavMessageContext.tsx

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/contexts/FavouriteMessageContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const FavouriteMessageContext = createContext<TContext | null>({
4545

4646
const FavouriteMessageProvider = ({ children }) => {
4747
const [favouriteMessageIds, dispatch] = useReducer(ActionHandler,
48-
JSON.parse(localStorage.getItem('io_element_favouriteMessages')) ?? []);
48+
JSON.parse(localStorage.getItem('io_element_favouriteMessages') ?? "[]"));
4949

5050
useEffect(() => {
5151
//if on page refresh cachedFavouriteMessageIds.length is 0, populate the array

test/components/views/messages/MessageActionBar-test.tsx

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ describe('<MessageActionBar />', () => {
8282
...mockClientMethodsEvents(),
8383
getRoom: jest.fn(),
8484
});
85+
86+
const localStorageMock = (() => {
87+
let store = {};
88+
return {
89+
getItem: jest.fn().mockImplementation(key => store[key]),
90+
setItem: jest.fn().mockImplementation((key, value) => {
91+
store[key] = value;
92+
}),
93+
clear: jest.fn().mockImplementation(() => {
94+
store = {};
95+
}),
96+
removeItem: jest.fn().mockImplementation((key) => delete store[key]),
97+
};
98+
})();
99+
Object.defineProperty(window, 'localStorage', {
100+
value: localStorageMock,
101+
writable: true,
102+
});
103+
85104
const room = new Room(roomId, client, userId);
86105
jest.spyOn(room, 'getPendingEvents').mockReturnValue([]);
87106

@@ -471,6 +490,20 @@ describe('<MessageActionBar />', () => {
471490
.mockImplementation(setting => setting === 'feature_favourite_messages');
472491
});
473492

493+
// it('favourite button is selected when clicked before', () => {
494+
// const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
495+
// act(() => {
496+
// fireEvent.click(queryByLabelText('Favourite'));
497+
// });
498+
// // expect(localStorageMock.setItem)
499+
// // .toHaveBeenCalled();
500+
// // expect(localStorageMock.getItem('io_element_favouriteMessages')).toContain(alicesMessageEvent.getId());
501+
502+
// // console.log(alicesMessageEvent.event.event_id);
503+
// expect(localStorageMock.setItem)
504+
// .toHaveBeenCalledWith('io_element_favouriteMessages', alicesMessageEvent.event.event_id);
505+
// });
506+
474507
it('renders favourite button on own actionable event', () => {
475508
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
476509
expect(queryByLabelText('Favourite')).toBeTruthy();
@@ -486,13 +519,26 @@ describe('<MessageActionBar />', () => {
486519
const { queryByLabelText } = getComponent({ mxEvent: redactedEvent });
487520
expect(queryByLabelText('Favourite')).toBeFalsy();
488521
});
522+
523+
it('changes style on click, handles get and set methods of localStorage', () => {
524+
const { queryByLabelText, container } = getComponent({ mxEvent: alicesMessageEvent });
525+
526+
//default state
527+
expect(container.getElementsByClassName('mx_MessageActionBar_favouriteButton_fillstar').length).toBe(0);
528+
expect(localStorageMock.getItem).toHaveBeenCalled();
529+
530+
//when clicked the star icon is styled and expects the setItem method of localstorage be called
531+
act(() => {
532+
fireEvent.click(queryByLabelText('Favourite'));
533+
});
534+
expect(container.getElementsByClassName('mx_MessageActionBar_favouriteButton_fillstar')).toBeTruthy();
535+
expect(localStorageMock.setItem).toHaveBeenCalled();
536+
});
489537
});
490538

491539
describe('when favourite_messages feature is disabled', () => {
492540
it('does not render', () => {
493-
jest.spyOn(SettingsStore, 'getValue')
494-
.mockImplementation(setting => setting === 'feature_favourite_messages')
495-
.mockReturnValue(false);
541+
jest.spyOn(SettingsStore, 'getValue').mockReturnValue(false);
496542
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
497543
expect(queryByLabelText('Favourite')).toBeFalsy();
498544
});

0 commit comments

Comments
 (0)