1+ import { EventCheck } from './event-check.model' ;
2+
13describe ( 'event check' , ( ) => {
4+ let eventCheck : EventCheck ;
5+ let origLog : any ;
6+ let logSpy : jasmine . Spy ;
7+
8+ function getEventLog ( ) : string {
9+ return eventCheck . view . eventLog . innerHTML ;
10+ }
11+
212 beforeAll ( ( ) => {
313 fixture . setBase ( 'src' ) ;
414 document . body . appendChild ( fixture . el ) ;
515 } ) ;
616
717 beforeEach ( ( ) => {
18+ origLog = console . log ;
19+ logSpy = spyOn ( console , 'log' ) ;
820 fixture . cleanup ( ) ;
921 fixture . load ( 'event-check.html' ) ;
22+ eventCheck = new EventCheck ( ) ;
23+ eventCheck . init ( ) ;
1024 } ) ;
1125
26+ afterEach ( ( ) => {
27+ console . log = origLog ;
28+ } )
29+
1230 it ( 'should start' , ( ) => expect ( ) . nothing ( ) ) ;
13- } ) ;
31+
32+ describe ( 'after write "a" into first input' , ( ) => {
33+ beforeEach ( ( ) => {
34+ const firstInput = eventCheck . view . firstInput ;
35+ firstInput . value = 'a' ;
36+ const event = new KeyboardEvent ( 'keydown' , { code : 'keyA' } ) ;
37+ console . log ( 'event' , event ) ;
38+ firstInput . dispatchEvent ( event ) ;
39+ return new Promise ( done => setTimeout ( done , 10 ) ) ;
40+ } ) ;
41+
42+ it ( 'log should contain entries' , ( ) => expect ( getEventLog ( ) ) . toContain ( 'keyA' ) ) ;
43+ } ) ;
44+
45+ describe ( 'after emulating tab' , ( ) => {
46+ beforeEach ( ( ) => {
47+ eventCheck . view . emulateTabButton . click ( ) ;
48+ } ) ;
49+
50+ it ( 'should have triggered all events' , ( ) => {
51+ const actualEventLog = normalizeEventLog ( getEventLog ( ) ) ;
52+ const expectedEventLog = normalizeEventLog ( `
53+ first-input (focus): focus
54+ first-input (keydown): Tab
55+ parent-of-first-input (keydown): Tab
56+ first-input (blur): blur
57+ second-input (focus): focus
58+ second-input (keyup): Tab
59+ parent-of-second-input (keyup): Tab
60+ ` ) ;
61+ expect ( actualEventLog ) . toEqual ( expectedEventLog ) ;
62+ expect ( eventCount ( actualEventLog ) ) . toEqual ( eventCount ( expectedEventLog ) ) ;
63+ } )
64+ } ) ;
65+ } ) ;
66+
67+ function normalizeEventLog ( log : string ) {
68+ let normalized = log . split ( / ( \s * \n ) + \s * / ) . join ( '' ) ;
69+ if ( ! normalized . startsWith ( '\n' ) ) {
70+ normalized = '\n' + normalized ;
71+ }
72+ return normalized ;
73+ }
74+
75+ function eventCount ( normalizedLog : string ) {
76+ return normalizedLog . split ( '\n' ) . length - 1 ;
77+ }
0 commit comments