5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { fakeAsync } from '@angular/core/testing' ;
9
-
10
8
import { BreakPoint } from './break-point' ;
11
9
import { validateSuffixes , mergeByAlias } from './breakpoint-tools' ;
12
10
import { DEFAULT_BREAKPOINTS } from './data/break-points' ;
13
11
import { ORIENTATION_BREAKPOINTS } from './data/orientation-break-points' ;
14
12
15
13
describe ( 'breakpoint-tools' , ( ) => {
16
- let all : BreakPoint [ ] ;
17
- let findByAlias = ( alias : string ) : BreakPoint | null => all . reduce ( ( pos : BreakPoint | null , it ) => {
18
- return pos || ( ( it . alias == alias ) ? it : null ) ;
19
- } , null ) ;
14
+ let allBreakPoints : BreakPoint [ ] = [ ] ;
15
+ const findByAlias = ( alias : string ) : BreakPoint => {
16
+ let result = { mediaQuery : '' , alias : '' } ;
17
+ allBreakPoints . forEach ( bp => {
18
+ if ( bp . alias === alias ) {
19
+ result = { ...bp } ;
20
+ }
21
+ } ) ;
22
+ return result ;
23
+ } ;
20
24
21
25
beforeEach ( ( ) => {
22
- all = DEFAULT_BREAKPOINTS . concat ( ORIENTATION_BREAKPOINTS ) ;
26
+ allBreakPoints = validateSuffixes ( [ ... DEFAULT_BREAKPOINTS , ... ORIENTATION_BREAKPOINTS ] ) ;
23
27
} ) ;
24
28
25
- describe ( 'validation' , ( ) => {
29
+ describe ( 'validation ' , ( ) => {
30
+
26
31
it ( 'should not replace an existing suffix' , ( ) => {
27
32
let validated = validateSuffixes ( [
28
33
{ alias : 'handset-portrait' , suffix : 'Handset' , mediaQuery : 'screen' }
29
34
] ) ;
30
35
expect ( validated [ 0 ] . suffix ) . toEqual ( 'Handset' ) ;
31
36
} ) ;
37
+
32
38
it ( 'should add valid suffixes to breakpoints' , ( ) => {
33
39
let validated = validateSuffixes ( [
34
40
{ alias : 'xs' , mediaQuery : 'screen and (max-width: 599px)' } ,
@@ -43,21 +49,19 @@ describe('breakpoint-tools', () => {
43
49
expect ( validated [ 3 ] . suffix ) . toEqual ( 'GtXs' ) ;
44
50
expect ( validated [ 4 ] . suffix ) . toEqual ( 'HandsetPortrait' ) ;
45
51
} ) ;
46
- it ( 'should auto-validate the DEFAULT_BREAKPOINTS' , ( ) => {
47
- fakeAsync ( ( ) => {
48
- let xsBp : BreakPoint = findByAlias ( 'xs' ) ! ;
49
- let gtLgBp : BreakPoint = findByAlias ( 'gt-lg' ) ! ;
50
- let xlBp : BreakPoint = findByAlias ( 'xl' ) ! ;
51
52
52
- expect ( xsBp . alias ) . toEqual ( 'xs' ) ;
53
- expect ( xsBp . suffix ) . toEqual ( 'Xs' ) ;
53
+ it ( 'should auto-validate the DEFAULT_BREAKPOINTS' , ( ) => {
54
+ let xsBp : BreakPoint = findByAlias ( 'xs' ) ;
55
+ expect ( xsBp . alias ) . toEqual ( 'xs' ) ;
56
+ expect ( xsBp . suffix ) . toEqual ( 'Xs' ) ;
54
57
55
- expect ( gtLgBp . alias ) . toEqual ( 'gt-lg' ) ;
56
- expect ( gtLgBp . suffix ) . toEqual ( 'GtLg' ) ;
58
+ let gtLgBp : BreakPoint = findByAlias ( 'gt-lg' ) ;
59
+ expect ( gtLgBp . alias ) . toEqual ( 'gt-lg' ) ;
60
+ expect ( gtLgBp . suffix ) . toEqual ( 'GtLg' ) ;
57
61
58
- expect ( xlBp . alias ) . toEqual ( 'xl' ) ;
59
- expect ( xlBp . suffix ) . toEqual ( 'Xl ' ) ;
60
- } ) ;
62
+ let xlBp : BreakPoint = findByAlias ( 'xl' ) ;
63
+ expect ( xlBp . alias ) . toEqual ( 'xl ' ) ;
64
+ expect ( xlBp . suffix ) . toEqual ( 'Xl' ) ;
61
65
} ) ;
62
66
} ) ;
63
67
@@ -67,19 +71,19 @@ describe('breakpoint-tools', () => {
67
71
{ alias : 'sm' , mediaQuery : 'screen' } ,
68
72
{ alias : 'md' , mediaQuery : 'screen' } ,
69
73
] ;
70
- all = mergeByAlias ( defaults , custom ) ;
74
+ allBreakPoints = mergeByAlias ( defaults , custom ) ;
71
75
72
- expect ( all . length ) . toEqual ( 2 ) ;
76
+ expect ( allBreakPoints . length ) . toEqual ( 2 ) ;
73
77
expect ( findByAlias ( 'sm' ) ! . suffix ) . toEqual ( 'Sm' ) ;
74
78
expect ( findByAlias ( 'md' ) ! . suffix ) . toEqual ( 'Md' ) ;
75
79
} ) ;
76
80
it ( 'should add custom breakpoints with unique aliases' , ( ) => {
77
81
let defaults = [ { alias : 'xs' , mediaQuery : 'screen and (max-width: 599px)' } ] ,
78
- custom = [ { alias : 'sm' , mediaQuery : 'screen' } , { alias : 'md' , mediaQuery : 'screen' } ] ;
82
+ custom = [ { alias : 'sm' , mediaQuery : 'screen' } , { alias : 'md' , mediaQuery : 'screen' } ] ;
79
83
80
- all = mergeByAlias ( defaults , custom ) ;
84
+ allBreakPoints = mergeByAlias ( defaults , custom ) ;
81
85
82
- expect ( all . length ) . toEqual ( 3 ) ;
86
+ expect ( allBreakPoints . length ) . toEqual ( 3 ) ;
83
87
expect ( findByAlias ( 'xs' ) ! . suffix ) . toEqual ( 'Xs' ) ;
84
88
expect ( findByAlias ( 'sm' ) ! . suffix ) . toEqual ( 'Sm' ) ;
85
89
expect ( findByAlias ( 'md' ) ! . suffix ) . toEqual ( 'Md' ) ;
@@ -88,9 +92,9 @@ describe('breakpoint-tools', () => {
88
92
let defaults = [ { alias : 'xs' , mediaQuery : 'screen and (max-width: 599px)' } ] ;
89
93
let custom = [ { alias : 'xs' , mediaQuery : 'screen and none' } ] ;
90
94
91
- all = mergeByAlias ( defaults , custom ) ;
95
+ allBreakPoints = mergeByAlias ( defaults , custom ) ;
92
96
93
- expect ( all . length ) . toEqual ( 1 ) ;
97
+ expect ( allBreakPoints . length ) . toEqual ( 1 ) ;
94
98
expect ( findByAlias ( 'xs' ) ! . suffix ) . toEqual ( 'Xs' ) ;
95
99
expect ( findByAlias ( 'xs' ) ! . mediaQuery ) . toEqual ( 'screen and none' ) ;
96
100
} ) ;
0 commit comments