22import deepFreeze from 'deep-freeze' ;
33
44import fullReducer from '../../boot/reducers' ;
5- import { getMute , getTopicVisibilityPolicy , isTopicVisibleInStream , reducer } from '../muteModel' ;
5+ import {
6+ getMute ,
7+ getTopicVisibilityPolicy ,
8+ isTopicVisible ,
9+ isTopicVisibleInStream ,
10+ reducer ,
11+ } from '../muteModel' ;
612import { EVENT , EVENT_MUTED_TOPICS } from '../../actionConstants' ;
713import * as eg from '../../__tests__/lib/exampleData' ;
814import { makeMuteState , makeUserTopic } from './mute-testlib' ;
@@ -31,6 +37,13 @@ describe('getters', () => {
3137 test ( 'with topic muted' , ( ) => {
3238 check ( makeMuteState ( [ [ eg . stream , 'topic' ] ] ) , UserTopicVisibilityPolicy . Muted ) ;
3339 } ) ;
40+
41+ test ( 'with topic unmuted' , ( ) => {
42+ check (
43+ makeMuteState ( [ [ eg . stream , 'topic' , UserTopicVisibilityPolicy . Unmuted ] ] ) ,
44+ UserTopicVisibilityPolicy . Unmuted ,
45+ ) ;
46+ } ) ;
3447 } ) ;
3548
3649 describe ( 'isTopicVisibleInStream' , ( ) => {
@@ -49,29 +62,81 @@ describe('getters', () => {
4962 test ( 'with topic muted' , ( ) => {
5063 check ( makeMuteState ( [ [ eg . stream , 'topic' ] ] ) , false ) ;
5164 } ) ;
65+
66+ test ( 'with topic unmuted' , ( ) => {
67+ check ( makeMuteState ( [ [ eg . stream , 'topic' , UserTopicVisibilityPolicy . Unmuted ] ] ) , true ) ;
68+ } ) ;
69+ } ) ;
70+
71+ describe ( 'isTopicVisible' , ( ) => {
72+ function check ( streamMuted , topicPolicy , expected ) {
73+ const subscription = { ...eg . subscription , in_home_view : ! streamMuted } ;
74+ const state = makeMuteState (
75+ topicPolicy === UserTopicVisibilityPolicy . None ? [ ] : [ [ eg . stream , 'topic' , topicPolicy ] ] ,
76+ ) ;
77+ expect ( isTopicVisible ( eg . stream . stream_id , 'topic' , subscription , state ) ) . toEqual ( expected ) ;
78+ }
79+
80+ test ( 'stream unmuted, topic-policy None' , ( ) => {
81+ check ( false , UserTopicVisibilityPolicy . None , true ) ;
82+ } ) ;
83+
84+ test ( 'stream unmuted, topic-policy Muted' , ( ) => {
85+ check ( false , UserTopicVisibilityPolicy . Muted , false ) ;
86+ } ) ;
87+
88+ test ( 'stream unmuted, topic-policy Unmuted' , ( ) => {
89+ check ( false , UserTopicVisibilityPolicy . Unmuted , true ) ;
90+ } ) ;
91+
92+ test ( 'stream muted, topic-policy None' , ( ) => {
93+ check ( true , UserTopicVisibilityPolicy . None , false ) ;
94+ } ) ;
95+
96+ test ( 'stream muted, topic-policy Muted' , ( ) => {
97+ check ( true , UserTopicVisibilityPolicy . Muted , false ) ;
98+ } ) ;
99+
100+ test ( 'stream muted, topic-policy Unmuted' , ( ) => {
101+ check ( true , UserTopicVisibilityPolicy . Unmuted , true ) ;
102+ } ) ;
52103 } ) ;
53104} ) ;
54105
55106describe ( 'reducer' , ( ) => {
56107 describe ( 'REGISTER_COMPLETE' , ( ) => {
57108 test ( 'in modern user_topics format: unit test' , ( ) => {
58109 const action = eg . mkActionRegisterComplete ( {
59- user_topics : [ makeUserTopic ( eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ) ] ,
110+ user_topics : [
111+ makeUserTopic ( eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ) ,
112+ makeUserTopic ( eg . stream , 'other topic' , UserTopicVisibilityPolicy . Unmuted ) ,
113+ ] ,
60114 } ) ;
61115 expect ( reducer ( initialState , action , eg . plusReduxState ) ) . toEqual (
62- makeMuteState ( [ [ eg . stream , 'topic' ] ] ) ,
116+ makeMuteState ( [
117+ [ eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ] ,
118+ [ eg . stream , 'other topic' , UserTopicVisibilityPolicy . Unmuted ] ,
119+ ] ) ,
63120 ) ;
64121 } ) ;
65122
66123 test ( 'in modern user_topics format: end-to-end test' , ( ) => {
67124 const action = eg . mkActionRegisterComplete ( {
68125 streams : [ eg . stream ] ,
69126 subscriptions : [ eg . subscription ] ,
70- user_topics : [ makeUserTopic ( eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ) ] ,
127+ user_topics : [
128+ makeUserTopic ( eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ) ,
129+ makeUserTopic ( eg . stream , 'other topic' , UserTopicVisibilityPolicy . Unmuted ) ,
130+ ] ,
71131 } ) ;
72132 const newState = tryGetActiveAccountState ( fullReducer ( eg . plusReduxState , action ) ) ;
73133 expect ( newState ) . toBeTruthy ( ) ;
74- expect ( newState && getMute ( newState ) ) . toEqual ( makeMuteState ( [ [ eg . stream , 'topic' ] ] ) ) ;
134+ expect ( newState && getMute ( newState ) ) . toEqual (
135+ makeMuteState ( [
136+ [ eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ] ,
137+ [ eg . stream , 'other topic' , UserTopicVisibilityPolicy . Unmuted ] ,
138+ ] ) ,
139+ ) ;
75140 } ) ;
76141
77142 test ( 'in old muted_topics format: unit test' , ( ) => {
@@ -117,21 +182,37 @@ describe('reducer', () => {
117182 check (
118183 initialState ,
119184 makeUserTopic ( eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ) ,
120- makeMuteState ( [ [ eg . stream , 'topic' ] ] ) ,
185+ makeMuteState ( [ [ eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ] ] ) ,
121186 ) ;
122187 } ) ;
123188
124189 test ( 'add in existing stream' , ( ) => {
125190 check (
126- makeMuteState ( [ [ eg . stream , 'topic' ] ] ) ,
127- makeUserTopic ( eg . stream , 'other topic' , UserTopicVisibilityPolicy . Muted ) ,
191+ makeMuteState ( [ [ eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ] ] ) ,
192+ makeUserTopic ( eg . stream , 'other topic' , UserTopicVisibilityPolicy . Unmuted ) ,
128193 makeMuteState ( [
129- [ eg . stream , 'topic' ] ,
130- [ eg . stream , 'other topic' ] ,
194+ [ eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ] ,
195+ [ eg . stream , 'other topic' , UserTopicVisibilityPolicy . Unmuted ] ,
131196 ] ) ,
132197 ) ;
133198 } ) ;
134199
200+ test ( 'change Muted -> Unmuted' , ( ) => {
201+ check (
202+ makeMuteState ( [ [ eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ] ] ) ,
203+ makeUserTopic ( eg . stream , 'topic' , UserTopicVisibilityPolicy . Unmuted ) ,
204+ makeMuteState ( [ [ eg . stream , 'topic' , UserTopicVisibilityPolicy . Unmuted ] ] ) ,
205+ ) ;
206+ } ) ;
207+
208+ test ( 'change Unmuted -> Muted' , ( ) => {
209+ check (
210+ makeMuteState ( [ [ eg . stream , 'topic' , UserTopicVisibilityPolicy . Unmuted ] ] ) ,
211+ makeUserTopic ( eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ) ,
212+ makeMuteState ( [ [ eg . stream , 'topic' , UserTopicVisibilityPolicy . Muted ] ] ) ,
213+ ) ;
214+ } ) ;
215+
135216 test ( 'remove, with others in stream' , ( ) => {
136217 check (
137218 makeMuteState ( [
0 commit comments