@@ -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