11import sinon from 'sinon' ;
22import { Poll , StreamChat } from '../../src' ;
33
4- import { describe , it , afterEach , expect } from 'vitest' ;
4+ import { describe , it , afterEach , expect , vi } from 'vitest' ;
55
66const pollId = 'WD4SBRJvLoGwB4oAoCQGM' ;
77
@@ -701,26 +701,26 @@ describe('Poll', () => {
701701 getPollStub . restore ( ) ;
702702 } ) ;
703703
704- it ( 'should remove oldest vote before casting a new one if reached max votes allowed' , async ( ) => {
704+ it ( 'should publish a notification and not cast vote if reached max votes allowed' , async ( ) => {
705705 const poll = new Poll ( {
706706 client,
707707 poll : { ...pollResponse , max_votes_allowed : user2Votes . length } ,
708708 } ) ;
709709 const option_id = 'ba933470-c0da-4b6f-a4d2-d2176ac0d4a8' ;
710710 const messageId = 'XXX' ;
711- const removePollVoteStub = sinon . stub ( client , 'removePollVote' ) ;
712- const castPollVoteStub = sinon . stub ( client , 'castPollVote' ) ;
713- removePollVoteStub . resolves ( 'removed' ) ;
714- castPollVoteStub . resolves ( { vote : { id : 'vote1' , option_id, user_id : 'user1' } } ) ;
711+ const removePollVoteSpy = vi
712+ . spyOn ( client , 'removePollVote' )
713+ . mockResolvedValue ( 'removed' ) ;
714+ const castPollVoteSpy = vi
715+ . spyOn ( client , 'castPollVote' )
716+ . mockResolvedValue ( { vote : { id : 'vote1' , option_id, user_id : 'user1' } } ) ;
717+ const addInfoNotificationSpy = vi . spyOn ( client . notifications , 'addInfo' ) ;
715718
716719 await poll . castVote ( option_id , messageId ) ;
717720
718- expect ( removePollVoteStub . calledWith ( messageId , pollResponse . id , user1Votes [ 1 ] . id ) ) . to
719- . be . true ;
720- expect ( castPollVoteStub . calledWith ( messageId , pollResponse . id , { option_id } ) ) . to . be
721- . true ;
722- removePollVoteStub . restore ( ) ;
723- castPollVoteStub . restore ( ) ;
721+ expect ( removePollVoteSpy ) . not . toHaveBeenCalled ( ) ;
722+ expect ( castPollVoteSpy ) . not . toHaveBeenCalled ( ) ;
723+ expect ( addInfoNotificationSpy ) . toHaveBeenCalledTimes ( 1 ) ;
724724 } ) ;
725725
726726 it ( 'should not remove oldest vote before casting a new one if not reached max votes allowed' , async ( ) => {
@@ -730,18 +730,21 @@ describe('Poll', () => {
730730 } ) ;
731731 const option_id = 'ba933470-c0da-4b6f-a4d2-d2176ac0d4a8' ;
732732 const messageId = 'XXX' ;
733- const removePollVoteStub = sinon . stub ( client , 'removePollVote' ) ;
734- const castPollVoteStub = sinon . stub ( client , 'castPollVote' ) ;
735- removePollVoteStub . resolves ( 'removed' ) ;
736- castPollVoteStub . resolves ( { vote : { id : 'vote1' , option_id, user_id : 'user1' } } ) ;
733+ const removePollVoteSpy = vi
734+ . spyOn ( client , 'removePollVote' )
735+ . mockResolvedValue ( 'removed' ) ;
736+ const castPollVoteSpy = vi
737+ . spyOn ( client , 'castPollVote' )
738+ . mockResolvedValue ( { vote : { id : 'vote1' , option_id, user_id : 'user1' } } ) ;
739+ const addInfoNotificationSpy = vi . spyOn ( client . notifications , 'addInfo' ) ;
737740
738741 await poll . castVote ( option_id , messageId ) ;
739742
740- expect ( removePollVoteStub . called ) . to . be . false ;
741- expect ( castPollVoteStub . calledWith ( messageId , pollResponse . id , { option_id } ) ) . to . be
742- . true ;
743- removePollVoteStub . restore ( ) ;
744- castPollVoteStub . restore ( ) ;
743+ expect ( removePollVoteSpy ) . not . toHaveBeenCalled ( ) ;
744+ expect ( castPollVoteSpy ) . toHaveBeenCalledWith ( messageId , pollResponse . id , {
745+ option_id ,
746+ } ) ;
747+ expect ( addInfoNotificationSpy ) . not . toHaveBeenCalled ( ) ;
745748 } ) ;
746749
747750 it ( 'should not remove oldest vote before casting a new one if max_votes_allowed is not defined' , async ( ) => {
@@ -751,17 +754,20 @@ describe('Poll', () => {
751754 } ) ;
752755 const option_id = 'ba933470-c0da-4b6f-a4d2-d2176ac0d4a8' ;
753756 const messageId = 'XXX' ;
754- const removePollVoteStub = sinon . stub ( client , 'removePollVote' ) ;
755- const castPollVoteStub = sinon . stub ( client , 'castPollVote' ) ;
756- removePollVoteStub . resolves ( 'removed' ) ;
757- castPollVoteStub . resolves ( { vote : { id : 'vote1' , option_id, user_id : 'user1' } } ) ;
757+ const removePollVoteSpy = vi
758+ . spyOn ( client , 'removePollVote' )
759+ . mockResolvedValue ( 'removed' ) ;
760+ const castPollVoteSpy = vi
761+ . spyOn ( client , 'castPollVote' )
762+ . mockResolvedValue ( { vote : { id : 'vote1' , option_id, user_id : 'user1' } } ) ;
763+ const addInfoNotificationSpy = vi . spyOn ( client . notifications , 'addInfo' ) ;
758764
759765 await poll . castVote ( option_id , messageId ) ;
760766
761- expect ( removePollVoteStub . called ) . to . be . false ;
762- expect ( castPollVoteStub . calledWith ( messageId , pollResponse . id , { option_id } ) ) . to . be
763- . true ;
764- removePollVoteStub . restore ( ) ;
765- castPollVoteStub . restore ( ) ;
767+ expect ( removePollVoteSpy ) . not . toHaveBeenCalled ( ) ;
768+ expect ( castPollVoteSpy ) . toHaveBeenCalledWith ( messageId , pollResponse . id , {
769+ option_id ,
770+ } ) ;
771+ expect ( addInfoNotificationSpy ) . not . toHaveBeenCalled ( ) ;
766772 } ) ;
767773} ) ;
0 commit comments