File size: 2,888 Bytes
13555f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import '@testing-library/jest-dom'
import {act, render} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import React from 'react'

import {wrapDNDIntl} from '../testUtils'

import ConfirmationDialogBox from './confirmationDialogBox'

describe('/components/confirmationDialogBox', () => {
    const dialogPropsWithCnfrmBtnText = {
        heading: 'test-heading',
        subText: 'test-sub-text',
        confirmButtonText: 'test-btn-text',
        onConfirm: jest.fn(),
        onClose: jest.fn(),
    }

    const dialogProps = {
        heading: 'test-heading',
        onConfirm: jest.fn(),
        onClose: jest.fn(),
    }

    it('confirmDialog should match snapshot', async () => {
        let container

        await act(async () => {
            const result = render(
                wrapDNDIntl(
                    <ConfirmationDialogBox
                        dialogBox={dialogPropsWithCnfrmBtnText}
                    />,
                ),
            )
            container = result.container
        })
        expect(container).toMatchSnapshot()
    })

    it('confirmDialog with Confirm Button Text should match snapshot', async () => {
        let containerWithCnfrmBtnText
        await act(async () => {
            const result = render(
                wrapDNDIntl(
                    <ConfirmationDialogBox
                        dialogBox={dialogPropsWithCnfrmBtnText}
                    />,
                ),
            )
            containerWithCnfrmBtnText = result.container
        })
        expect(containerWithCnfrmBtnText).toMatchSnapshot()
    })

    it('confirm button click, run onConfirm Function once', () => {
        const result = render(
            wrapDNDIntl(<ConfirmationDialogBox dialogBox={dialogProps}/>),
        )

        userEvent.click(result.getByTitle('Confirm'))
        expect(dialogProps.onConfirm).toBeCalledTimes(1)
    })

    it('confirm button (with passed prop text), run onConfirm Function once', () => {
        const resultWithConfirmBtnText = render(
            wrapDNDIntl(
                <ConfirmationDialogBox
                    dialogBox={dialogPropsWithCnfrmBtnText}
                />,
            ),
        )

        userEvent.click(
            resultWithConfirmBtnText.getByTitle(dialogPropsWithCnfrmBtnText.confirmButtonText),
        )

        expect(dialogPropsWithCnfrmBtnText.onConfirm).toBeCalledTimes(1)
    })

    it('cancel button click runs onClose function', () => {
        const result = render(wrapDNDIntl(
            <ConfirmationDialogBox
                dialogBox={dialogProps}
            />,
        ))

        userEvent.click(result.getByTitle('Cancel'))
        expect(dialogProps.onClose).toBeCalledTimes(1)
    })
})