1
1
import sinon from 'sinon' ;
2
2
import { Poll , StreamChat } from '../../src' ;
3
3
4
- import { describe , it , afterEach , expect } from 'vitest' ;
4
+ import { describe , it , afterEach , expect , vi } from 'vitest' ;
5
5
6
6
const pollId = 'WD4SBRJvLoGwB4oAoCQGM' ;
7
7
@@ -701,26 +701,26 @@ describe('Poll', () => {
701
701
getPollStub . restore ( ) ;
702
702
} ) ;
703
703
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 ( ) => {
705
705
const poll = new Poll ( {
706
706
client,
707
707
poll : { ...pollResponse , max_votes_allowed : user2Votes . length } ,
708
708
} ) ;
709
709
const option_id = 'ba933470-c0da-4b6f-a4d2-d2176ac0d4a8' ;
710
710
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' ) ;
715
718
716
719
await poll . castVote ( option_id , messageId ) ;
717
720
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 ) ;
724
724
} ) ;
725
725
726
726
it ( 'should not remove oldest vote before casting a new one if not reached max votes allowed' , async ( ) => {
@@ -730,18 +730,21 @@ describe('Poll', () => {
730
730
} ) ;
731
731
const option_id = 'ba933470-c0da-4b6f-a4d2-d2176ac0d4a8' ;
732
732
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' ) ;
737
740
738
741
await poll . castVote ( option_id , messageId ) ;
739
742
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 ( ) ;
745
748
} ) ;
746
749
747
750
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', () => {
751
754
} ) ;
752
755
const option_id = 'ba933470-c0da-4b6f-a4d2-d2176ac0d4a8' ;
753
756
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' ) ;
758
764
759
765
await poll . castVote ( option_id , messageId ) ;
760
766
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 ( ) ;
766
772
} ) ;
767
773
} ) ;
0 commit comments