1+ import type { Observable } from 'rxjs' ;
2+
3+ import { Subject } from 'rxjs' ;
4+ import { MockElement , MockEvent } from '../test-support' ;
5+ import { Swap , swap } from './swap-source' ;
6+
7+ describe ( 'Swap Event Adapter' , ( ) => {
8+
9+ it ( 'Swaps a value in an element with a static string' , ( ) => {
10+ const oldValue = 'old data' ;
11+ const newValue = 'new data' ;
12+
13+ const el = MockElement ( {
14+ tagName : 'INPUT' ,
15+ type : 'text' ,
16+ value : oldValue ,
17+ } ) ;
18+
19+ const eventData = MockEvent ( 'input' , {
20+ target : el as HTMLInputElement
21+ } ) ;
22+
23+ const handlerSpy = jest . fn ( ) ;
24+ const source = Swap ( newValue ) ( handlerSpy ) ;
25+ source . next ( eventData ) ;
26+
27+ expect ( handlerSpy ) . toHaveBeenCalledWith ( oldValue ) ;
28+ expect ( el . value ) . toEqual ( newValue ) ;
29+ } ) ;
30+
31+ it ( 'Swaps a value in an element using a function' , ( ) => {
32+ const oldValue = 'old data' ;
33+
34+ const el = MockElement ( {
35+ tagName : 'INPUT' ,
36+ type : 'text' ,
37+ value : oldValue ,
38+ } ) ;
39+
40+ const eventData = MockEvent ( 'input' , {
41+ target : el as HTMLInputElement
42+ } ) ;
43+
44+ const replaceFn = ( v : string ) => v . toUpperCase ( ) ;
45+
46+ const handlerSpy = jest . fn ( ) ;
47+ const source = Swap ( replaceFn ) ( handlerSpy ) ;
48+ source . next ( eventData ) ;
49+
50+ expect ( handlerSpy ) . toHaveBeenCalledWith ( oldValue ) ;
51+ expect ( el . value ) . toEqual ( 'OLD DATA' ) ;
52+ } ) ;
53+
54+ it ( 'Swaps a value in an element with empty string by default' , ( ) => {
55+ const oldValue = 'old data' ;
56+
57+ const el = MockElement ( {
58+ tagName : 'INPUT' ,
59+ type : 'text' ,
60+ value : oldValue ,
61+ } ) ;
62+
63+ const eventData = MockEvent ( 'input' , {
64+ target : el as HTMLInputElement
65+ } ) ;
66+
67+ const handlerSpy = jest . fn ( ) ;
68+ const source = Swap ( ) ( handlerSpy ) ;
69+ source . next ( eventData ) ;
70+
71+ expect ( handlerSpy ) . toHaveBeenCalledWith ( oldValue ) ;
72+ expect ( el . value ) . toEqual ( '' ) ;
73+ } ) ;
74+
75+ } ) ;
76+
77+ describe ( 'swap Event Operator' , ( ) => {
78+
79+ it ( 'Swaps and emits a value from an element with static string' , ( ) => {
80+ const oldValue = 'old data' ;
81+ const newValue = 'new data' ;
82+
83+ const el = MockElement ( {
84+ tagName : 'INPUT' ,
85+ type : 'text' ,
86+ value : oldValue ,
87+ } ) ;
88+
89+ const eventData = MockEvent ( 'input' , {
90+ target : el as HTMLInputElement
91+ } ) ;
92+
93+ const handlerSpy = jest . fn ( ) ;
94+ const pipeline = new Subject < typeof eventData > ( ) . pipe ( swap ( newValue ) ) as Observable < string > & Subject < typeof eventData > ;
95+ pipeline . subscribe ( x => handlerSpy ( x ) ) ;
96+ pipeline . next ( eventData ) ;
97+
98+ expect ( handlerSpy ) . toHaveBeenCalledWith ( oldValue ) ;
99+ expect ( el . value ) . toEqual ( newValue ) ;
100+ } ) ;
101+
102+ it ( 'Swaps and emits a value from an element using function' , ( ) => {
103+ const oldValue = 'old data' ;
104+
105+ const el = MockElement ( {
106+ tagName : 'INPUT' ,
107+ type : 'text' ,
108+ value : oldValue ,
109+ } ) ;
110+
111+ const eventData = MockEvent ( 'input' , {
112+ target : el as HTMLInputElement
113+ } ) ;
114+
115+ const replaceFn = ( v : string ) => v . toUpperCase ( ) ;
116+
117+ const handlerSpy = jest . fn ( ) ;
118+ const pipeline = new Subject < typeof eventData > ( ) . pipe ( swap ( replaceFn ) ) as Observable < string > & Subject < typeof eventData > ;
119+ pipeline . subscribe ( x => handlerSpy ( x ) ) ;
120+ pipeline . next ( eventData ) ;
121+
122+ expect ( handlerSpy ) . toHaveBeenCalledWith ( oldValue ) ;
123+ expect ( el . value ) . toEqual ( 'OLD DATA' ) ;
124+ } ) ;
125+
126+ } ) ;
0 commit comments