@@ -2,10 +2,14 @@ import { render, screen } from '@testing-library/react'
22import { createRef } from 'react'
33import { describe , expect , it , vi } from 'vitest'
44
5- import { BG_GRADIENT_ID , DEFAULT_BGCOLOR , DEFAULT_SIZE } from './constants'
5+ import { BG_GRADIENT_ID , DEFAULT_BGCOLOR , DEFAULT_SIZE , GRADIENT_ID } from './constants'
66import { ReactQRCode } from './react-qr-code'
77import type {
88 BackgroundSettings ,
9+ DataModulesSettings ,
10+ FinderPatternInnerSettings ,
11+ FinderPatternOuterSettings ,
12+ GradientSettings ,
913 GradientSettingsType ,
1014 ReactQRCodeRef ,
1115} from './types/lib'
@@ -44,7 +48,57 @@ describe('ReactQRCode', () => {
4448 expect ( svg ) . toHaveAttribute ( 'aria-label' , customLabel )
4549 } )
4650
47- describe ( 'background' , ( ) => {
51+ describe ( 'Data modules' , ( ) => {
52+ it ( 'renders the path with correct color' , ( ) => {
53+ const dataModulesSettings : DataModulesSettings = {
54+ color : '#560bad' ,
55+ }
56+ render ( < ReactQRCode value = 'test' dataModulesSettings = { dataModulesSettings } /> )
57+
58+ const path = screen . getByTestId ( 'data-modules' )
59+
60+ expect ( path ) . toBeInTheDocument ( )
61+ expect ( path ) . toHaveAttribute ( 'fill' , dataModulesSettings . color )
62+ } )
63+ } )
64+
65+ describe ( 'Finder patterns outer' , ( ) => {
66+ it ( 'renders the path with correct color' , ( ) => {
67+ const finderPatternOuterSettings : FinderPatternOuterSettings = {
68+ color : '#560bad' ,
69+ }
70+ render (
71+ < ReactQRCode
72+ value = 'test'
73+ finderPatternOuterSettings = { finderPatternOuterSettings }
74+ /> ,
75+ )
76+
77+ screen . getAllByTestId ( 'finder-patterns-outer' ) . forEach ( ( path ) => {
78+ expect ( path ) . toHaveAttribute ( 'fill' , finderPatternOuterSettings . color )
79+ } )
80+ } )
81+ } )
82+
83+ describe ( 'Finder patterns inner' , ( ) => {
84+ it ( 'renders the path with correct color' , ( ) => {
85+ const finderPatternInnerSettings : FinderPatternInnerSettings = {
86+ color : '#560bad' ,
87+ }
88+ render (
89+ < ReactQRCode
90+ value = 'test'
91+ finderPatternInnerSettings = { finderPatternInnerSettings }
92+ /> ,
93+ )
94+
95+ screen . getAllByTestId ( 'finder-patterns-inner' ) . forEach ( ( path ) => {
96+ expect ( path ) . toHaveAttribute ( 'fill' , finderPatternInnerSettings . color )
97+ } )
98+ } )
99+ } )
100+
101+ describe ( 'Background' , ( ) => {
48102 it ( 'renders with default background color' , ( ) => {
49103 render ( < ReactQRCode value = 'test' /> )
50104
@@ -74,10 +128,10 @@ describe('ReactQRCode', () => {
74128 }
75129 const { container } = render ( < ReactQRCode value = 'test' background = { gradient } /> )
76130
77- const background = screen . getByTestId ( 'background' )
78- const stops = container . querySelectorAll ( `${ selector } stop` )
131+ const backgroundPath = screen . getByTestId ( 'background' )
132+ const stops = container . querySelectorAll ( `${ selector } # ${ BG_GRADIENT_ID } stop` )
79133
80- expect ( background ) . toHaveAttribute ( 'fill' , `url(#${ BG_GRADIENT_ID } )` )
134+ expect ( backgroundPath ) . toHaveAttribute ( 'fill' , `url(#${ BG_GRADIENT_ID } )` )
81135 expect ( container . querySelector ( `${ selector } ` ) ) . toBeInTheDocument ( )
82136 expect ( stops ) . toHaveLength ( 2 )
83137 expect ( stops [ 0 ] ) . toHaveAttribute ( 'stop-color' , gradient . stops [ 0 ] . color )
@@ -87,6 +141,44 @@ describe('ReactQRCode', () => {
87141 } )
88142 } )
89143
144+ describe ( 'QR code data gradient' , ( ) => {
145+ it . each ( [
146+ [ 'linear' , 'linearGradient' ] ,
147+ [ 'radial' , 'radialGradient' ] ,
148+ ] ) ( 'renders with %s gradient for QR code data' , ( type , selector ) => {
149+ const gradient : GradientSettings = {
150+ type : type as GradientSettingsType ,
151+ rotation : 0 ,
152+ stops : [
153+ { offset : '0%' , color : '#4568DC' } ,
154+ { offset : '100%' , color : '#B06AB3' } ,
155+ ] ,
156+ }
157+ const { container } = render ( < ReactQRCode value = 'test' gradient = { gradient } /> )
158+
159+ const gradientElement = container . querySelector ( `${ selector } #${ GRADIENT_ID } ` )
160+ const stops = container . querySelectorAll ( `${ selector } #${ GRADIENT_ID } stop` )
161+
162+ expect ( gradientElement ) . toBeInTheDocument ( )
163+ expect ( screen . getByTestId ( 'data-modules' ) ) . toHaveAttribute (
164+ 'fill' ,
165+ `url(#${ GRADIENT_ID } )` ,
166+ )
167+ screen . getAllByTestId ( 'finder-patterns-outer' ) . forEach ( ( path ) => {
168+ expect ( path ) . toHaveAttribute ( 'fill' , `url(#${ GRADIENT_ID } )` )
169+ } )
170+ screen . getAllByTestId ( 'finder-patterns-inner' ) . forEach ( ( path ) => {
171+ expect ( path ) . toHaveAttribute ( 'fill' , `url(#${ GRADIENT_ID } )` )
172+ } )
173+
174+ expect ( stops ) . toHaveLength ( 2 )
175+ expect ( stops [ 0 ] ) . toHaveAttribute ( 'stop-color' , gradient . stops [ 0 ] . color )
176+ expect ( stops [ 0 ] ) . toHaveAttribute ( 'offset' , gradient . stops [ 0 ] . offset )
177+ expect ( stops [ 1 ] ) . toHaveAttribute ( 'stop-color' , gradient . stops [ 1 ] . color )
178+ expect ( stops [ 1 ] ) . toHaveAttribute ( 'offset' , gradient . stops [ 1 ] . offset )
179+ } )
180+ } )
181+
90182 describe ( 'Image settings' , ( ) => {
91183 it ( 'renders with image settings' , ( ) => {
92184 const imageSettings = {
0 commit comments