|
| 1 | +import { MockProvider } from 'ng-mocks'; |
| 2 | + |
1 | 3 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 4 | +import { Router } from '@angular/router'; |
2 | 5 |
|
3 | 6 | import { TruncatedTextComponent } from './truncated-text.component'; |
4 | 7 |
|
5 | | -import { TranslateServiceMock } from '@testing/mocks/translate.service.mock'; |
| 8 | +import { provideOSFCore } from '@testing/osf.testing.provider'; |
| 9 | +import { RouterMockBuilder, RouterMockType } from '@testing/providers/router-provider.mock'; |
6 | 10 |
|
7 | 11 | describe('TruncatedTextComponent', () => { |
8 | 12 | let component: TruncatedTextComponent; |
9 | 13 | let fixture: ComponentFixture<TruncatedTextComponent>; |
| 14 | + let mockRouter: RouterMockType; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + mockRouter = RouterMockBuilder.create().build(); |
10 | 18 |
|
11 | | - beforeEach(async () => { |
12 | | - await TestBed.configureTestingModule({ |
| 19 | + TestBed.configureTestingModule({ |
13 | 20 | imports: [TruncatedTextComponent], |
14 | | - providers: [TranslateServiceMock], |
15 | | - }).compileComponents(); |
| 21 | + providers: [provideOSFCore(), MockProvider(Router, mockRouter)], |
| 22 | + }); |
16 | 23 |
|
17 | 24 | fixture = TestBed.createComponent(TruncatedTextComponent); |
18 | 25 | component = fixture.componentInstance; |
19 | 26 | }); |
20 | 27 |
|
21 | 28 | it('should create', () => { |
| 29 | + fixture.detectChanges(); |
22 | 30 | expect(component).toBeTruthy(); |
23 | 31 | }); |
24 | 32 |
|
25 | | - it('should set text input correctly', () => { |
26 | | - fixture.componentRef.setInput('text', 'Test text content'); |
27 | | - expect(component.text()).toBe('Test text content'); |
28 | | - }); |
29 | | - |
30 | | - it('should set maxVisibleLines input correctly', () => { |
| 33 | + it('should reflect text, maxVisibleLines, and expanded inputs', () => { |
| 34 | + fixture.componentRef.setInput('text', 'hello'); |
31 | 35 | fixture.componentRef.setInput('maxVisibleLines', 5); |
| 36 | + fixture.componentRef.setInput('expanded', true); |
| 37 | + |
| 38 | + expect(component.text()).toBe('hello'); |
32 | 39 | expect(component.maxVisibleLines()).toBe(5); |
| 40 | + expect(component.expanded()).toBe(true); |
33 | 41 | }); |
34 | 42 |
|
35 | | - it('should call checkTextOverflow in ngAfterViewInit', () => { |
36 | | - const checkTextOverflowSpy = jest.spyOn(component as any, 'checkTextOverflow'); |
| 43 | + it('should derive isExpanded from expanded input or user expansion', () => { |
| 44 | + expect(component.isExpanded()).toBe(false); |
37 | 45 |
|
38 | | - component.ngAfterViewInit(); |
| 46 | + fixture.componentRef.setInput('expanded', true); |
| 47 | + expect(component.isExpanded()).toBe(true); |
39 | 48 |
|
40 | | - expect(checkTextOverflowSpy).toHaveBeenCalledTimes(1); |
| 49 | + fixture.componentRef.setInput('expanded', false); |
| 50 | + component.isTextExpanded.set(true); |
| 51 | + expect(component.isExpanded()).toBe(true); |
41 | 52 | }); |
42 | 53 |
|
43 | | - it('should handle empty text input', () => { |
44 | | - fixture.componentRef.setInput('text', ''); |
45 | | - expect(component.text()).toBe(''); |
| 54 | + it('should compute buttonLabel for read-more vs hide when not navigating', () => { |
| 55 | + fixture.componentRef.setInput('navigateOnReadMore', false); |
| 56 | + fixture.componentRef.setInput('readMoreLabel', 'read'); |
| 57 | + fixture.componentRef.setInput('hideLabel', 'hide'); |
| 58 | + |
| 59 | + expect(component.buttonLabel()).toBe('read'); |
| 60 | + |
| 61 | + component.isTextExpanded.set(true); |
| 62 | + expect(component.buttonLabel()).toBe('hide'); |
46 | 63 | }); |
47 | 64 |
|
48 | | - it('should handle different maxVisibleLines values', () => { |
49 | | - const values = [1, 2, 3, 5, 10, 100]; |
| 65 | + it('should use readMoreLabel for buttonLabel when navigateOnReadMore is true', () => { |
| 66 | + fixture.componentRef.setInput('navigateOnReadMore', true); |
| 67 | + fixture.componentRef.setInput('readMoreLabel', 'go'); |
50 | 68 |
|
51 | | - values.forEach((value) => { |
52 | | - fixture.componentRef.setInput('maxVisibleLines', value); |
53 | | - expect(component.maxVisibleLines()).toBe(value); |
54 | | - }); |
| 69 | + expect(component.buttonLabel()).toBe('go'); |
| 70 | + |
| 71 | + component.isTextExpanded.set(true); |
| 72 | + expect(component.buttonLabel()).toBe('go'); |
55 | 73 | }); |
56 | 74 |
|
57 | | - it('should handle negative maxVisibleLines', () => { |
58 | | - fixture.componentRef.setInput('maxVisibleLines', -1); |
59 | | - expect(component.maxVisibleLines()).toBe(-1); |
| 75 | + it('should call checkTextOverflow from ngAfterViewInit', () => { |
| 76 | + const checkTextOverflowSpy = jest.spyOn(component, 'checkTextOverflow'); |
| 77 | + |
| 78 | + component.ngAfterViewInit(); |
| 79 | + |
| 80 | + expect(checkTextOverflowSpy).toHaveBeenCalledTimes(1); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should reset user expansion when text changes to a new non-empty value', () => { |
| 84 | + jest.useFakeTimers(); |
| 85 | + try { |
| 86 | + fixture.componentRef.setInput('text', 'first'); |
| 87 | + fixture.detectChanges(); |
| 88 | + jest.advanceTimersByTime(0); |
| 89 | + |
| 90 | + component.isTextExpanded.set(true); |
| 91 | + fixture.componentRef.setInput('text', 'second'); |
| 92 | + fixture.detectChanges(); |
| 93 | + jest.advanceTimersByTime(0); |
| 94 | + |
| 95 | + expect(component.isTextExpanded()).toBe(false); |
| 96 | + } finally { |
| 97 | + jest.useRealTimers(); |
| 98 | + } |
60 | 99 | }); |
61 | 100 |
|
62 | | - it('should properly update isTextExpanded signal', () => { |
63 | | - expect(component['isTextExpanded']()).toBe(false); |
| 101 | + it('should toggle isTextExpanded on handleButtonClick when not navigateOnReadMore', () => { |
| 102 | + fixture.componentRef.setInput('navigateOnReadMore', false); |
| 103 | + |
| 104 | + expect(component.isTextExpanded()).toBe(false); |
64 | 105 |
|
65 | | - component['isTextExpanded'].set(true); |
66 | | - expect(component['isTextExpanded']()).toBe(true); |
| 106 | + component.handleButtonClick(); |
| 107 | + expect(component.isTextExpanded()).toBe(true); |
67 | 108 |
|
68 | | - component['isTextExpanded'].set(false); |
69 | | - expect(component['isTextExpanded']()).toBe(false); |
| 109 | + component.handleButtonClick(); |
| 110 | + expect(component.isTextExpanded()).toBe(false); |
70 | 111 | }); |
71 | 112 |
|
72 | | - it('should properly update hasOverflowingText signal', () => { |
73 | | - expect(component['hasOverflowingText']()).toBe(false); |
| 113 | + it('should navigate when navigateOnReadMore is true', () => { |
| 114 | + fixture.componentRef.setInput('navigateOnReadMore', true); |
| 115 | + fixture.componentRef.setInput('link', ['/preprints', '123']); |
74 | 116 |
|
75 | | - component['hasOverflowingText'].set(true); |
76 | | - expect(component['hasOverflowingText']()).toBe(true); |
| 117 | + component.handleButtonClick(); |
77 | 118 |
|
78 | | - component['hasOverflowingText'].set(false); |
79 | | - expect(component['hasOverflowingText']()).toBe(false); |
| 119 | + expect(mockRouter.navigate).toHaveBeenCalledWith(['/preprints', '123']); |
80 | 120 | }); |
81 | 121 | }); |
0 commit comments