Skip to content

Commit 436381e

Browse files
add tests
1 parent 3ca1704 commit 436381e

File tree

5 files changed

+90
-22
lines changed

5 files changed

+90
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(ns status-im2.contexts.chat.messages.content.audio.component-spec
2+
(:require [status-im2.contexts.chat.messages.content.audio.view :as audio-message]
3+
[test-helpers.component :as h]
4+
[react-native.audio-toolkit :as audio]
5+
[re-frame.core :as re-frame]))
6+
7+
(defonce message
8+
{:audio-duration-ms 5000
9+
:message-id "message-id"})
10+
11+
(defonce context
12+
{:in-pinned-view? false})
13+
14+
(defn setup-subs
15+
[subs]
16+
(doseq [keyval subs]
17+
(re-frame/reg-sub
18+
(key keyval)
19+
(fn [_] (val keyval)))))
20+
21+
(h/describe "audio message"
22+
(h/before-each
23+
#(setup-subs {:mediaserver/port 1000}))
24+
25+
(h/test "renders correctly"
26+
(h/render [audio-message/audio-message message context])
27+
(h/is-truthy (h/get-by-label-text :audio-message-container)))
28+
29+
(h/test "press play calls audio/toggle-playpause-player"
30+
(with-redefs [audio/toggle-playpause-player (js/jest.fn)
31+
audio/new-player (fn [_ _ _] {})
32+
audio/destroy-player #()
33+
audio/prepare-player (fn [_ on-success _] (on-success))
34+
audio-message/download-audio-http (fn [_ on-success] (on-success "audio-uri"))]
35+
(h/render [audio-message/audio-message message context])
36+
(h/fire-event
37+
:on-press
38+
(h/get-by-label-text :play-pause-audio-message-button))
39+
(-> (h/expect audio/toggle-playpause-player)
40+
(.toHaveBeenCalledTimes 1))))
41+
42+
(h/test "press play renders error"
43+
(h/render [audio-message/audio-message message context])
44+
(with-redefs [audio/toggle-playpause-player (fn [_ _ _ on-error] (on-error))
45+
audio/new-player (fn [_ _ _] {})
46+
audio/destroy-player #()
47+
audio/prepare-player (fn [_ on-success _] (on-success))
48+
audio-message/download-audio-http (fn [_ on-success] (on-success "audio-uri"))]
49+
(h/fire-event
50+
:on-press
51+
(h/get-by-label-text :play-pause-audio-message-button))
52+
(h/wait-for #(h/is-truthy (h/get-by-label-text :audio-error-label))))))

src/status_im2/contexts/chat/messages/content/audio/view.cljs

+23-21
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@
2121
(defonce progress-timer (atom nil))
2222
(defonce current-player-key (reagent/atom nil))
2323

24-
(defn- get-player-key
24+
(defn get-player-key
2525
[message-id in-pinned-view?]
2626
(str in-pinned-view? message-id))
2727

28-
(defn- destroy-player
28+
(defn destroy-player
2929
[player-key]
3030
(when-let [player (@active-players player-key)]
3131
(audio/destroy-player player)
3232
(swap! active-players dissoc player-key)))
3333

34-
(defn- update-state
34+
(defn update-state
3535
[state new-state]
3636
(when-not (= @state new-state)
3737
(reset! state new-state)))
3838

39-
(defn- seek-player
39+
(defn seek-player
4040
[player-key player-state value on-success]
4141
(when-let [player (@active-players player-key)]
4242
(audio/seek-player
@@ -46,14 +46,14 @@
4646
#(update-state player-state :error))
4747
(update-state player-state :seeking)))
4848

49-
(defn- download-audio-http
49+
(defn download-audio-http
5050
[base64-uri on-success]
5151
(-> (.config ReactNativeBlobUtil (clj->js {:trusty platform/ios?}))
5252
(.fetch "GET" (str base64-uri))
5353
(.then #(on-success (.base64 ^js %)))
5454
(.catch #(log/error "could not fetch audio " base64-uri))))
5555

56-
(defn- create-player
56+
(defn create-player
5757
[{:keys [progress-ref player-state player-key]} audio-url on-success]
5858
(download-audio-http
5959
audio-url
@@ -75,7 +75,7 @@
7575
#(update-state player-state :error)))))
7676
(update-state player-state :preparing))
7777

78-
(defn- play-pause-player
78+
(defn play-pause-player
7979
[{:keys [player-key player-state progress-ref message-id audio-duration-ms seeking-audio?
8080
user-interaction?]
8181
:as params}]
@@ -113,6 +113,7 @@
113113
(reset! progress-timer nil)))
114114
#(update-state player-state :error))
115115
(do
116+
(swap! audio-uris assoc player-key audio-uri)
116117
(destroy-player player-key)
117118
(create-player params
118119
audio-uri
@@ -126,8 +127,7 @@
126127
player-state
127128
checked-seek-time
128129
#(play-pause-player params)))
129-
(play-pause-player params))))))
130-
(swap! audio-uris assoc player-key audio-uri)))
130+
(play-pause-player params))))))))
131131

132132
(defn audio-message
133133
[{:keys [audio-duration-ms message-id]}
@@ -168,24 +168,26 @@
168168
:weight :medium
169169
:size :paragraph-2}
170170
(i18n/label :error-loading-audio)]
171-
[rn/view {:style (style/container)}
171+
[rn/view
172+
{:accessibility-label :audio-message-container
173+
:style (style/container)}
172174
[rn/touchable-opacity
173-
{:on-press #(play-pause-player {:player-key player-key
174-
:player-state player-state
175-
:progress-ref progress
176-
:message-id message-id
177-
:audio-duration-ms duration
178-
:seeking-audio? seeking-audio?
179-
:user-interaction? true})
180-
:style (style/play-pause-container)}
175+
{:accessibility-label :play-pause-audio-message-button
176+
:on-press #(play-pause-player {:player-key player-key
177+
:player-state player-state
178+
:progress-ref progress
179+
:message-id message-id
180+
:audio-duration-ms duration
181+
:seeking-audio? seeking-audio?
182+
:user-interaction? true})
183+
:style (style/play-pause-container)}
181184
[quo/icon
182185
(case @player-state
183186
:preparing :i/loading
184187
:playing :i/pause-audio
185188
:i/play-audio)
186-
{:size 20
187-
:accessibility-label :play-pause-audio-message-button
188-
:color colors/white}]]
189+
{:size 20
190+
:color colors/white}]]
189191
[quo/soundtrack
190192
{:style style/slider-container
191193
:audio-current-time-ms progress

src/status_im2/core_spec.cljs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
(ns status-im2.core-spec
22
(:require
3-
[status-im2.contexts.communities.menus.community-options.component-spec]))
3+
[status-im2.contexts.communities.menus.community-options.component-spec]
4+
[status-im2.contexts.chat.messages.content.audio.component-spec]))

src/test_helpers/component.cljs

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363

6464
(def within rtl/within)
6565

66+
(defn wait-for
67+
([condition] (wait-for condition {}))
68+
([condition options]
69+
(rtl/waitFor condition (clj->js options))))
70+
6671
(defn fire-event
6772
([event-name node]
6873
(fire-event event-name node nil))

test/jest/jestSetup.js

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ jest.mock("i18n-js", () => ({
7070
t: (label) => `tx:${label}`
7171
}));
7272

73+
jest.mock("react-native-blob-util", () => ({
74+
default: {
75+
config: jest.fn().mockReturnValue({
76+
fetch: jest.fn()
77+
})
78+
}
79+
}));
80+
7381
NativeModules.ReactLocalization = {
7482
language: 'en',
7583
locale: 'en',

0 commit comments

Comments
 (0)