|
| 1 | +import { Component, DebugElement } from '@angular/core'; |
| 2 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | +import { By } from '@angular/platform-browser'; |
| 4 | +import { Cloudinary } from './cloudinary.service'; |
| 5 | +import CloudinaryConfiguration from './cloudinary-configuration.class'; |
| 6 | +import { CloudinaryBackgroundImageDirective } from './cloudinary-background-image.directive' |
| 7 | +import { CloudinaryTransformationDirective } from './cloudinary-transformation.directive'; |
| 8 | + |
| 9 | +@Component({ |
| 10 | + template: ` |
| 11 | + <button [clBackgroundImage]="image.public_id" fetch_format="auto" target="_blank"> |
| 12 | + <cl-transformation effect="sepia"></cl-transformation> |
| 13 | + click me |
| 14 | + </button> |
| 15 | + <img [clBackgroundImage]="image.public_id" width="100" crop="scale"/> |
| 16 | + ` |
| 17 | +}) |
| 18 | +class TestComponent { } |
| 19 | + |
| 20 | +describe('CloudinaryBackgroundImageDirective', () => { |
| 21 | + |
| 22 | + let fixture: ComponentFixture<TestComponent>; |
| 23 | + let des: DebugElement[]; // the three elements w/ the directive |
| 24 | + let localCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'), |
| 25 | + { cloud_name: '@@fake_angular2_sdk@@' } as CloudinaryConfiguration); |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + fixture = TestBed.configureTestingModule({ |
| 29 | + declarations: [CloudinaryBackgroundImageDirective, CloudinaryTransformationDirective, TestComponent], |
| 30 | + providers: [{ provide: Cloudinary, useValue: localCloudinary }] |
| 31 | + }).createComponent(TestComponent); |
| 32 | + const comp = fixture.componentInstance; |
| 33 | + |
| 34 | + // pretend that it was wired to something that supplied an image |
| 35 | + comp['image'] = { |
| 36 | + public_id: 'some_image_id' |
| 37 | + }; |
| 38 | + |
| 39 | + fixture.detectChanges(); // initial binding |
| 40 | + |
| 41 | + // all elements with an attached CloudinaryImageSourceDirective |
| 42 | + des = fixture.debugElement.queryAll(By.directive(CloudinaryBackgroundImageDirective)); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should have 1 element', () => { |
| 46 | + expect(des.length).toBe(2); |
| 47 | + }); |
| 48 | + |
| 49 | + it('updates the background image with transformations', () => { |
| 50 | + const button = des[0].nativeElement as HTMLButtonElement; |
| 51 | + expect(button.style.backgroundImage).toEqual(jasmine.stringMatching(/image\/upload\/e_sepia\/f_auto\/some_image_id/)); |
| 52 | + expect(button.textContent).toEqual(jasmine.stringMatching(/click me/)); |
| 53 | + }); |
| 54 | + |
| 55 | + it('updates the background image without transformations', () => { |
| 56 | + const div = des[1].nativeElement as HTMLDivElement; |
| 57 | + expect(div.style.backgroundImage).toEqual(jasmine.stringMatching(/c_scale,w_100\/some_image_id/)); |
| 58 | + }); |
| 59 | +}); |
0 commit comments