about summary refs log tree commit diff
path: root/__tests__/view/com/composer/ComposePost.test.tsx
blob: 84377f62ff58d049417e137ee2cd580c14908e37 (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import React from 'react'
import {ComposePost} from '../../../../src/view/com/composer/ComposePost'
import {cleanup, fireEvent, render, waitFor} from '../../../../jest/test-utils'
import * as apilib from '../../../../src/state/lib/api'
import {
  mockedAutocompleteViewStore,
  mockedRootStore,
} from '../../../../__mocks__/state-mock'
import Toast from 'react-native-root-toast'

describe('ComposePost', () => {
  const mockedProps = {
    replyTo: {
      uri: 'testUri',
      cid: 'testCid',
      text: 'testText',
      author: {
        handle: 'test.handle',
        displayName: 'test name',
        avatar: '',
      },
    },
    onPost: jest.fn(),
    onClose: jest.fn(),
  }

  afterAll(() => {
    jest.clearAllMocks()
    cleanup()
  })

  it('renders post composer', async () => {
    const {findByTestId} = render(<ComposePost {...mockedProps} />)
    const composePostView = await findByTestId('composePostView')
    expect(composePostView).toBeTruthy()
  })

  it('closes composer', async () => {
    const {findByTestId} = render(<ComposePost {...mockedProps} />)
    const composerCancelButton = await findByTestId('composerCancelButton')
    fireEvent.press(composerCancelButton)
    expect(mockedProps.onClose).toHaveBeenCalled()
  })

  it('changes text and publishes post', async () => {
    const postSpy = jest.spyOn(apilib, 'post').mockResolvedValue({
      uri: '',
      cid: '',
    })
    const toastSpy = jest.spyOn(Toast, 'show')

    const wrapper = render(<ComposePost {...mockedProps} />)

    const composerTextInput = await wrapper.findByTestId('composerTextInput')
    fireEvent.changeText(composerTextInput, 'testing publish')

    const composerPublishButton = await wrapper.findByTestId(
      'composerPublishButton',
    )
    fireEvent.press(composerPublishButton)

    expect(postSpy).toHaveBeenCalledWith(
      mockedRootStore,
      'testing publish',
      'testUri',
      [],
      new Set<string>(),
      expect.anything(),
    )

    // Waits for request to be resolved
    await waitFor(() => {
      expect(mockedProps.onPost).toHaveBeenCalled()
      expect(mockedProps.onClose).toHaveBeenCalled()
      expect(toastSpy).toHaveBeenCalledWith('Your reply has been published', {
        animation: true,
        duration: 3500,
        hideOnPress: true,
        position: 50,
        shadow: true,
      })
    })
  })

  it('selects autocomplete item', async () => {
    jest
      .spyOn(React, 'useMemo')
      .mockReturnValueOnce(mockedAutocompleteViewStore)

    const {findAllByTestId} = render(<ComposePost {...mockedProps} />)
    const autocompleteButton = await findAllByTestId('autocompleteButton')

    fireEvent.press(autocompleteButton[0])
    expect(mockedAutocompleteViewStore.setActive).toHaveBeenCalledWith(false)
  })

  it('selects photos', async () => {
    const {findByTestId, queryByTestId} = render(
      <ComposePost {...mockedProps} />,
    )
    let photoCarouselPickerView = queryByTestId('photoCarouselPickerView')
    expect(photoCarouselPickerView).toBeFalsy()

    const composerSelectPhotosButton = await findByTestId(
      'composerSelectPhotosButton',
    )
    fireEvent.press(composerSelectPhotosButton)

    photoCarouselPickerView = await findByTestId('photoCarouselPickerView')
    expect(photoCarouselPickerView).toBeTruthy()

    fireEvent.press(composerSelectPhotosButton)

    photoCarouselPickerView = queryByTestId('photoCarouselPickerView')
    expect(photoCarouselPickerView).toBeFalsy()
  })
})