11import "@testing-library/jest-dom" ;
2- import { render , waitFor } from "@testing-library/react" ;
2+ import { act , render , waitFor , screen } from "@testing-library/react" ;
33import { createElement } from "react" ;
44import { Dimensions } from "@mendix/pluggable-widgets-commons" ;
55import { NotFoundException } from "@zxing/library/cjs" ;
@@ -66,53 +66,18 @@ describe("Barcode scanner", () => {
6666 describe ( "shows an appropriate error to the user" , ( ) => {
6767 it ( "in the form of text when a generic error occurs" , async ( ) => {
6868 useReaderMock . mockImplementationOnce ( ( args : any ) => {
69- setTimeout ( ( ) => args . onError ( new Error ( "this is unexpected error" ) ) , 100 ) ;
69+ setTimeout ( ( ) => args . onError ( new Error ( "some error message " ) ) , 100 ) ;
7070 } ) ;
7171 mockGetUserMedia ( jest . fn ( ) ) ;
7272
73- const { container } = render ( < BarcodeScanner class = "" showMask { ...dimensions } /> ) ;
74-
75- await waitFor ( ( ) =>
76- expect ( container ) . toHaveTextContent (
77- "Error in barcode scanner: an unexpected error occurred while retrieving the camera media stream."
78- )
79- ) ;
80- } ) ;
81-
82- it ( "in the form of text when no device was found" , async ( ) => {
83- useReaderMock . mockImplementationOnce ( ( args : any ) => {
84- setTimeout ( ( ) => {
85- const error = new Error ( "This is an error message" ) ;
86- error . name = "NotFoundError" ;
87- args . onError ( error ) ;
88- } , 100 ) ;
73+ await act ( async ( ) => {
74+ render ( < BarcodeScanner class = "" showMask { ...dimensions } /> ) ;
8975 } ) ;
90-
91- mockGetUserMedia ( jest . fn ( ) ) ;
92-
93- const { container } = render ( < BarcodeScanner class = "" showMask { ...dimensions } /> ) ;
94-
9576 await waitFor ( ( ) =>
96- expect ( container ) . toHaveTextContent ( "Error in barcode scanner: no camera media devices were found. " )
77+ expect ( screen . getByText ( / E r r o r / i ) ) . toHaveTextContent ( "Error in barcode scanner: some error message " )
9778 ) ;
9879 } ) ;
9980
100- it ( "not in the form of text since that is handled by the design when the users denies access to the camera" , async ( ) => {
101- useReaderMock . mockImplementationOnce ( ( args : any ) => {
102- setTimeout ( ( ) => {
103- const error = new Error ( "This is an error message" ) ;
104- error . name = "NotAllowedError" ;
105- args . onError ( error ) ;
106- } , 100 ) ;
107- } ) ;
108-
109- mockGetUserMedia ( jest . fn ( ) ) ;
110-
111- const { container } = render ( < BarcodeScanner class = "" showMask { ...dimensions } /> ) ;
112-
113- await waitFor ( ( ) => expect ( container ) . not . toHaveTextContent ( / E r r o r i n b a r c o d e s c a n n e r : / ) ) ;
114- } ) ;
115-
11681 it ( "in the form of text when the code scanner unexpectedly fails" , async ( ) => {
11782 useReaderMock . mockImplementationOnce ( ( args : any ) => {
11883 setTimeout ( ( ) => {
@@ -122,11 +87,12 @@ describe("Barcode scanner", () => {
12287
12388 mockGetUserMedia ( jest . fn ( ) ) ;
12489
125- const { container } = render ( < BarcodeScanner class = "" showMask { ...dimensions } /> ) ;
126-
90+ await act ( async ( ) => {
91+ render ( < BarcodeScanner class = "" showMask { ...dimensions } /> ) ;
92+ } ) ;
12793 await waitFor ( ( ) =>
128- expect ( container ) . toHaveTextContent (
129- "Error in barcode scanner: an unexpected error occurred while detecting a barcode in the camera media stream. "
94+ expect ( screen . getByText ( / E r r o r / i ) ) . toHaveTextContent (
95+ "Error in barcode scanner: Unable to decode from stream"
13096 )
13197 ) ;
13298 } ) ;
0 commit comments