import sinon from "sinon" ; import { pureMerge } from "coral-common/utils" ; import { GQLResolver } from "coral-framework/schema" ; import { act , createResolversStub , CreateTestRendererParams , waitForElement , within , } from "coral-framework/testHelpers" ; import { commenters , settings , stories , viewerPassive } from "../fixtures" ; import create from "./create" ; const story = stories [ 0 ] ; const viewer = viewerPassive ; async function createTestRenderer ( params = { } ) { const { testRenderer , context } = create ( { ... params , resolvers : pureMerge ( createResolversStub < GQLResolver > ( { Query : { settings : ( ) => settings , viewer : ( ) => viewer , stream : ( ) => story , } , } ) , params . resolvers ) , initLocalState : ( localRecord , source , environment ) => { localRecord . setValue ( "PREFERENCES" , "profileTab" ) ; if ( params . initLocalState ) { params . initLocalState ( localRecord , source , environment ) ; } } , } ) ; const ignoredCommenters = await waitForElement ( ( ) => within ( testRenderer . root ) . queryByTestID ( "profile-account-ignoredCommenters" ) ) ; return { testRenderer , context , ignoredCommenters , } ; } it ( "render notifications form" , async ( ) => { const updateNotificationSettings = sinon . stub ( ) . callsFake ( ( _ , { input : { clientMutationId , ... notifications } } ) => { expectAndFail ( notifications ) . toMatchObject ( { onReply : true , onFeatured : true , onStaffReplies : true , onModeration : true , digestFrequency : "HOURLY" , } ) ; return { user : pureMerge < typeof viewer > ( viewer , { notifications , } ) , clientMutationId , } ; } ) ; const { testRenderer } = await createTestRenderer ( { resolvers : createResolversStub < GQLResolver > ( { Mutation : { updateNotificationSettings , } , } ) , } ) ; const container = await waitForElement ( ( ) => within ( testRenderer . root ) . getByTestID ( "profile-account-notifications" ) ) ; expect ( await within ( container ) . axe ( ) ) . toHaveNoViolations ( ) ; const form = within ( container ) . getByType ( "form" ) ; const onReply = await waitForElement ( ( ) => within ( form ) . getByID ( "onReply" , { exact : false } ) ) ; const onStaffReplies = await waitForElement ( ( ) => within ( form ) . getByID ( "onStaffReplies" , { exact : false } ) ) ; const onModeration = await waitForElement ( ( ) => within ( form ) . getByID ( "onModeration" , { exact : false } ) ) ; const onFeatured = await waitForElement ( ( ) => within ( form ) . getByID ( "onFeatured" , { exact : false } ) ) ; const digestFrequency = await waitForElement ( ( ) => within ( form ) . getByID ( "digestFrequency" , { exact : false } ) ) ; const save = await waitForElement ( ( ) => within ( form ) . getByType ( "button" ) ) ; expect ( save . props . disabled ) . toEqual ( true ) ; expect ( digestFrequency . props . disabled ) . toEqual ( true ) ; act ( ( ) => { onReply . props . onChange ( true ) ; onStaffReplies . props . onChange ( true ) ; onModeration . props . onChange ( true ) ; onFeatured . props . onChange ( true ) ; } ) ; expect ( digestFrequency . props . disabled ) . toEqual ( false ) ; act ( ( ) => { digestFrequency . props . onChange ( "HOURLY" ) ; } ) ; await act ( async ( ) => { await form . props . onSubmit ( ) ; } ) ; expect ( updateNotificationSettings . calledOnce ) . toEqual ( true ) ; expect ( save . props . disabled ) . toEqual ( true ) ; act ( ( ) => { onReply . props . onChange ( false ) ; } ) ; expect ( save . props . disabled ) . toEqual ( false ) ; act ( ( ) => { onReply . props . onChange ( true ) ; } ) ; expect ( save . props . disabled ) . toEqual ( true ) ; } ) ; it ( "render empty ignored users list" , async ( ) => { const { ignoredCommenters } = await createTestRenderer ( ) ; const editButton = within ( ignoredCommenters ) . getByText ( "Manage" ) ; act ( ( ) => { editButton . props . onClick ( ) ; } ) ; await waitForElement ( ( ) => within ( ignoredCommenters ) . getByText ( "You are not currently ignoring anyone" , { exact : false , } ) ) ; } ) ; it ( "render ignored users list" , async ( ) => { const { ignoredCommenters } = await createTestRenderer ( { resolvers : createResolversStub < GQLResolver > ( { Query : { viewer : ( ) => pureMerge < typeof viewer > ( viewer , { ignoredUsers : [ commenters [ 0 ] , commenters [ 1 ] ] , } ) , } , Mutation : { removeUserIgnore : ( { variables } ) => { expectAndFail ( variables ) . toMatchObject ( { userID : commenters [ 1 ] . id , } ) ; return { } ; } , } , } ) , } ) ; const editButton = within ( ignoredCommenters ) . getByText ( "Manage" ) ; act ( ( ) => { editButton . props . onClick ( ) ; } ) ; within ( ignoredCommenters ) . getByText ( commenters [ 0 ] . username ! ) ; within ( ignoredCommenters ) . getByText ( commenters [ 1 ] . username ! ) ; const stopIgnoreButtons = within ( ignoredCommenters ) . getAllByText ( "Stop ignoring" , { selector : "button" } ) ; await act ( async ( ) => { stopIgnoreButtons [ 0 ] . props . onClick ( ) ; } ) ; await waitForElement ( ( ) => within ( ignoredCommenters ) . getByText ( "You are no longer ignoring" ) ) ; within ( ignoredCommenters ) . getByText ( commenters [ 0 ] . username ! ) ; } ) ;