|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import {Component} from '@angular/core'; |
| 9 | +import {CommonModule} from '@angular/common'; |
| 10 | +import {ComponentFixture, TestBed, inject} from '@angular/core/testing'; |
| 11 | + |
| 12 | +import {DEFAULT_BREAKPOINTS_PROVIDER} from '../../media-query/breakpoints/break-points-provider'; |
| 13 | +import {BreakPointRegistry} from '../../media-query/breakpoints/break-point-registry'; |
| 14 | +import {MockMatchMedia} from '../../media-query/mock/mock-match-media'; |
| 15 | +import {MatchMedia} from '../../media-query/match-media'; |
| 16 | +import {FlexLayoutModule} from '../../module'; |
| 17 | + |
| 18 | +import {customMatchers, expect} from '../../utils/testing/custom-matchers'; |
| 19 | +import {_dom as _} from '../../utils/testing/dom-tools'; |
| 20 | + |
| 21 | +import { |
| 22 | + makeExpectDOMFrom, |
| 23 | + makeCreateTestComponent, |
| 24 | + queryFor |
| 25 | +} from '../../utils/testing/helpers'; |
| 26 | + |
| 27 | +describe('flex directive', () => { |
| 28 | + let fixture: ComponentFixture<any>; |
| 29 | + let matchMedia: MockMatchMedia; |
| 30 | + let expectDOMFrom = makeExpectDOMFrom(() => TestFlexComponent); |
| 31 | + let componentWithTemplate = (template: string) => { |
| 32 | + fixture = makeCreateTestComponent(() => TestFlexComponent)(template); |
| 33 | + |
| 34 | + inject([MatchMedia], (_matchMedia: MockMatchMedia) => { |
| 35 | + matchMedia = _matchMedia; |
| 36 | + })(); |
| 37 | + }; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + jasmine.addMatchers(customMatchers); |
| 41 | + |
| 42 | + // Configure testbed to prepare services |
| 43 | + TestBed.configureTestingModule({ |
| 44 | + imports: [CommonModule, FlexLayoutModule], |
| 45 | + declarations: [TestFlexComponent], |
| 46 | + providers: [ |
| 47 | + BreakPointRegistry, DEFAULT_BREAKPOINTS_PROVIDER, |
| 48 | + {provide: MatchMedia, useClass: MockMatchMedia} |
| 49 | + ] |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('with static features', () => { |
| 54 | + |
| 55 | + it('should add correct styles for default `fxFlexOffset` usage', () => { |
| 56 | + componentWithTemplate(`<div fxFlexOffset='32px' fxFlex></div>`); |
| 57 | + fixture.detectChanges(); |
| 58 | + |
| 59 | + let dom = fixture.debugElement.children[0].nativeElement; |
| 60 | + let isBox = _.hasStyle(dom, 'margin-left', '32px'); |
| 61 | + let hasFlex = _.hasStyle(dom, 'flex', '1 1 1e-09px') || // IE |
| 62 | + _.hasStyle(dom, 'flex', '1 1 1e-9px') || // Chrome |
| 63 | + _.hasStyle(dom, 'flex', '1 1 0.000000001px') || // Safari |
| 64 | + _.hasStyle(dom, 'flex', '1 1 0px'); |
| 65 | + |
| 66 | + expect(isBox).toBeTruthy(); |
| 67 | + expect(hasFlex).toBe(true); |
| 68 | + }); |
| 69 | + |
| 70 | + |
| 71 | + it('should work with percentage values', () => { |
| 72 | + expectDOMFrom(`<div fxFlexOffset='17' fxFlex='37'></div>`).toHaveCssStyle({ |
| 73 | + 'flex': '1 1 100%', |
| 74 | + 'box-sizing': 'border-box', |
| 75 | + 'margin-left': '17%' |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should work fxLayout parents', () => { |
| 80 | + componentWithTemplate(` |
| 81 | + <div fxLayout='column' class='test'> |
| 82 | + <div fxFlex='30px' fxFlexOffset='17px'> </div> |
| 83 | + </div> |
| 84 | + `); |
| 85 | + fixture.detectChanges(); |
| 86 | + let parent = queryFor(fixture, '.test')[0].nativeElement; |
| 87 | + let element = queryFor(fixture, '[fxFlex]')[0].nativeElement; |
| 88 | + |
| 89 | + // parent flex-direction found with 'column' with child height styles |
| 90 | + expect(parent).toHaveCssStyle({'flex-direction': 'column', 'display': 'flex'}); |
| 91 | + expect(element).toHaveCssStyle({'margin-top': '17px'}); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should CSS stylesheet and not inject flex-direction on parent', () => { |
| 95 | + componentWithTemplate(` |
| 96 | + <style> |
| 97 | + .test { flex-direction:column; display: flex; } |
| 98 | + </style> |
| 99 | + <div class='test'> |
| 100 | + <div fxFlexOffset='41px' fxFlex='30px'></div> |
| 101 | + </div> |
| 102 | + `); |
| 103 | + |
| 104 | + fixture.detectChanges(); |
| 105 | + let parent = queryFor(fixture, '.test')[0].nativeElement; |
| 106 | + let element = queryFor(fixture, '[fxFlex]')[0].nativeElement; |
| 107 | + |
| 108 | + // parent flex-direction found with 'column' with child height styles |
| 109 | + expect(parent).toHaveCssStyle({'flex-direction': 'column', 'display': 'flex'}); |
| 110 | + expect(element).toHaveCssStyle({'margin-top': '41px'}); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should work with styled-parent flex directions', () => { |
| 114 | + componentWithTemplate(` |
| 115 | + <div fxLayout='row'> |
| 116 | + <div style='flex-direction:column' class='parent'> |
| 117 | + <div fxFlex='60px' fxFlexOffset='21'> </div> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + `); |
| 121 | + fixture.detectChanges(); |
| 122 | + let element = queryFor(fixture, '[fxFlex]')[0].nativeElement; |
| 123 | + let parent = queryFor(fixture, '.parent')[0].nativeElement; |
| 124 | + |
| 125 | + // parent flex-direction found with 'column'; set child with height styles |
| 126 | + expect(element).toHaveCssStyle({'margin-top': '21%'}); |
| 127 | + expect(parent).toHaveCssStyle({'flex-direction': 'column'}); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should ignore fxLayout settings on same element', () => { |
| 131 | + expectDOMFrom(` |
| 132 | + <div fxLayout='column' fxFlex='37%' fxFlexOffset='52px' > |
| 133 | + </div> |
| 134 | + `) |
| 135 | + .not.toHaveCssStyle({ |
| 136 | + 'flex-direction': 'row', |
| 137 | + 'flex': '1 1 100%', |
| 138 | + 'margin-left': '52px', |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + }); |
| 143 | + |
| 144 | +}); |
| 145 | + |
| 146 | + |
| 147 | +// ***************************************************************** |
| 148 | +// Template Component |
| 149 | +// ***************************************************************** |
| 150 | + |
| 151 | +@Component({ |
| 152 | + selector: 'test-component-shell', |
| 153 | + template: `<span>PlaceHolder Template HTML</span>` |
| 154 | +}) |
| 155 | +export class TestFlexComponent { |
| 156 | + public direction = 'column'; |
| 157 | +} |
| 158 | + |
| 159 | + |
0 commit comments