Guest User

Untitled

a guest
Jul 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. import { SelectComponent, Option } from './select.component';
  2. import { TestBed, async, ComponentFixture } from '@angular/core/testing';
  3. import { NO_ERRORS_SCHEMA } from '@angular/core';
  4. import { By } from '@angular/platform-browser';
  5.  
  6. let selectComponent: SelectComponent;
  7. let options: Option[];
  8. describe('Select Component', () => {
  9. beforeEach(() => {
  10. selectComponent = new SelectComponent();
  11. options = [
  12. { id: 1, name: 'a', value: 'a'},
  13. { id: 2, name: 'b', value: 'b'},
  14. { id: 3, name: 'c', value: 'c'}
  15. ];
  16. selectComponent.options = [...options ];
  17.  
  18. });
  19.  
  20. it('should load', () => {
  21. expect(selectComponent).toBeDefined();
  22. });
  23.  
  24. describe('Select simple', () => {
  25.  
  26. it('should be simple', () => {
  27. expect(selectComponent.multiple).toBe(false);
  28. });
  29.  
  30. it('should initialize with value null', () => {
  31. selectComponent.writeValue(null);
  32. expect(selectComponent.getValues()).toEqual(undefined);
  33. selectComponent.defineFieldLabel();
  34. expect(selectComponent.fieldLabel).toBe(selectComponent.placeholder);
  35. });
  36.  
  37. it('should initialize with 1 value', () => {
  38. const expected = options[0];
  39. selectComponent.writeValue(options[0]);
  40. const result = selectComponent.getValues() as Option;
  41. expect(result.id).toEqual(expected.id);
  42.  
  43. selectComponent.defineFieldLabel();
  44. expect(selectComponent.fieldLabel).toBe(expected.name);
  45. });
  46.  
  47. it('should toggle select content', () => {
  48. selectComponent.toggleContent();
  49. expect(selectComponent.open).toBe(true);
  50. selectComponent.toggleContent();
  51. expect(selectComponent.open).toBe(false);
  52. selectComponent.toggleContent();
  53. expect(selectComponent.open).toBe(true);
  54. });
  55.  
  56. it('select an option', () => {
  57. selectComponent.selectOption(options[0]);
  58. expect((selectComponent.getValues() as Option).id).toBe(1);
  59.  
  60. selectComponent.defineFieldLabel();
  61. expect(selectComponent.fieldLabel).toBe(options[0].name);
  62.  
  63. selectComponent.selectOption(options[0]);
  64. expect((selectComponent.getValues() as Option).name).toBe(options[0].name);
  65.  
  66. selectComponent.defineFieldLabel();
  67. expect(selectComponent.fieldLabel).toBe(options[0].name);
  68. });
  69.  
  70. });
  71.  
  72.  
  73. describe('Select multiple', () => {
  74. beforeEach(() => {
  75. selectComponent.multiple = true;
  76. });
  77.  
  78. it('should be simple', () => {
  79. expect(selectComponent.multiple).toBe(true);
  80. });
  81.  
  82. it('should initialize with value null', () => {
  83. selectComponent.writeValue(null);
  84. expect(selectComponent.getValues()).toEqual([]);
  85. selectComponent.defineFieldLabel();
  86. expect(selectComponent.fieldLabel).toBe(selectComponent.placeholder);
  87. });
  88.  
  89. it('should initialize with 1 value', () => {
  90. const expected = options[0];
  91. selectComponent.writeValue([options[0], options[1]]);
  92.  
  93. const results = selectComponent.getValues() as Option[];
  94.  
  95. expect(results[0].id).toEqual(expected.id);
  96. expect(results[1].id).toEqual(options[1].id);
  97. expect(results.length).toBe(2);
  98.  
  99. selectComponent.defineFieldLabel();
  100. expect(selectComponent.fieldLabel).toBe('Selected sites (2)');
  101. });
  102.  
  103. it('should initialize values', () => {
  104. selectComponent.writeValue([ ...options]);
  105. selectComponent.defineFieldLabel();
  106. expect(selectComponent.fieldLabel).toBe('All Sites selected');
  107. });
  108.  
  109. it('should toggle select content', () => {
  110. selectComponent.toggleContent();
  111. expect(selectComponent.open).toBe(true);
  112. selectComponent.toggleContent();
  113. expect(selectComponent.open).toBe(false);
  114. selectComponent.toggleContent();
  115. expect(selectComponent.open).toBe(true);
  116. });
  117. });
  118.  
  119. describe('shallow Test', () => {
  120.  
  121. let fixture: ComponentFixture<SelectComponent>;
  122. let comp: SelectComponent;
  123.  
  124. beforeEach(() => {
  125. TestBed.configureTestingModule({
  126. declarations: [
  127. SelectComponent
  128. ],
  129. providers: [],
  130. schemas: [ NO_ERRORS_SCHEMA ]
  131. });
  132. });
  133.  
  134. beforeEach(async(() => {
  135. TestBed.compileComponents().then(() => {
  136. fixture = TestBed.createComponent(SelectComponent);
  137. comp = fixture.componentInstance;
  138. });
  139. }));
  140.  
  141. it('should have Select Placeholder', () => {
  142. fixture.detectChanges();
  143. expect(fixture.nativeElement.querySelector('.lbl').textContent).toBe('Select');
  144. });
  145. it('should open and close on Select an Item', () => {
  146. comp.options = [
  147. {id: 1, name: 'Option a'},
  148. {id: 2, name: 'Option b'}
  149. ];
  150. fixture.detectChanges();
  151. expect(fixture.debugElement.query(By.css('.lbl-content')).nativeElement.textContent.trim()).toBe('Select an Item');
  152. expect(fixture.componentInstance.open).toBe(false);
  153.  
  154. fixture.debugElement.query(By.css('.lbl-content')).nativeElement.click();
  155.  
  156. expect(fixture.componentInstance.open).toBe(true);
  157.  
  158. fixture.debugElement.query(By.css('.slct-option-simple')).nativeElement.click();
  159. expect(fixture.componentInstance.open).toBe(false);
  160. fixture.detectChanges();
  161. expect(fixture.debugElement.query(By.css('.slc-value')).nativeElement.textContent).toBe('Option a');
  162. });
  163. });
  164.  
  165.  
  166. });
Add Comment
Please, Sign In to add comment