-
Notifications
You must be signed in to change notification settings - Fork 4
Implement CpsRootFontSizeService
#603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fateeand
wants to merge
5
commits into
master
Choose a base branch
from
578-implement-cpsrootfontsizeservice
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
248 changes: 248 additions & 0 deletions
248
projects/cps-ui-kit/src/lib/services/cps-root-font-size/cps-root-font-size.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| import { TestBed } from '@angular/core/testing'; | ||
| import { DOCUMENT } from '@angular/common'; | ||
| import { PLATFORM_ID } from '@angular/core'; | ||
| import { | ||
| CpsRootFontSizeService, | ||
| CPS_ROOT_FONT_SIZE_SERVICE | ||
| } from './cps-root-font-size.service'; | ||
|
|
||
| const SENTINEL_ATTR = 'data-cps-root-font-size-sentinel'; | ||
|
|
||
| describe('CpsRootFontSizeService', () => { | ||
| let service: CpsRootFontSizeService; | ||
| let document: Document; | ||
| let resizeCallback: (entries: unknown[], observer: unknown) => void; | ||
| let mockObserve: jest.Mock; | ||
| let mockDisconnect: jest.Mock; | ||
| let computedFontSize: string; | ||
|
|
||
| beforeEach(() => { | ||
| computedFontSize = '16px'; | ||
| mockObserve = jest.fn(); | ||
| mockDisconnect = jest.fn(); | ||
|
|
||
| (globalThis as any).ResizeObserver = jest.fn( | ||
| (cb: (entries: unknown[], observer: unknown) => void) => { | ||
| resizeCallback = cb; | ||
| return { observe: mockObserve, disconnect: mockDisconnect }; | ||
| } | ||
| ); | ||
|
|
||
| jest | ||
| .spyOn(window, 'getComputedStyle') | ||
| .mockReturnValue({ fontSize: computedFontSize } as CSSStyleDeclaration); | ||
|
|
||
| TestBed.configureTestingModule({}); | ||
| service = TestBed.inject(CpsRootFontSizeService); | ||
| document = TestBed.inject(DOCUMENT); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| service.ngOnDestroy(); | ||
| jest.restoreAllMocks(); | ||
| delete (globalThis as any).ResizeObserver; | ||
| document | ||
| .querySelectorAll(`[${SENTINEL_ATTR}]`) | ||
| .forEach((el) => el.remove()); | ||
| }); | ||
|
|
||
| it('should be created', () => { | ||
| expect(service).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('should initialize fontSize from getComputedStyle', () => { | ||
| expect(service.fontSize()).toBe(16); | ||
| }); | ||
|
|
||
| it('should append a sentinel element to the document root', () => { | ||
| const sentinel = document.querySelector(`[${SENTINEL_ATTR}]`); | ||
| expect(sentinel).not.toBeNull(); | ||
| expect(document.documentElement.contains(sentinel)).toBe(true); | ||
| }); | ||
|
|
||
| it('should set sentinel style to width:1rem and be hidden', () => { | ||
| const sentinel = document.querySelector<HTMLElement>(`[${SENTINEL_ATTR}]`)!; | ||
| expect(sentinel.style.width).toBe('1rem'); | ||
| expect(sentinel.style.height).toBe('0px'); | ||
| expect(sentinel.style.visibility).toBe('hidden'); | ||
| expect(sentinel.style.position).toBe('absolute'); | ||
| expect(sentinel.style.pointerEvents).toBe('none'); | ||
| }); | ||
|
|
||
| it('should start observing the sentinel element', () => { | ||
| expect(mockObserve).toHaveBeenCalledTimes(1); | ||
| const sentinel = document.querySelector(`[${SENTINEL_ATTR}]`); | ||
| expect(mockObserve).toHaveBeenCalledWith(sentinel); | ||
| }); | ||
|
|
||
| it('should update fontSize signal when ResizeObserver fires with a new size', () => { | ||
| (window.getComputedStyle as jest.Mock).mockReturnValue({ | ||
| fontSize: '20px' | ||
| } as CSSStyleDeclaration); | ||
|
|
||
| resizeCallback([], null as unknown as ResizeObserver); | ||
|
|
||
| expect(service.fontSize()).toBe(20); | ||
| }); | ||
|
|
||
| it('should not update fontSize signal when the size has not changed', () => { | ||
| resizeCallback([], null as unknown as ResizeObserver); | ||
| expect(service.fontSize()).toBe(16); | ||
| }); | ||
|
|
||
| describe('ngOnDestroy', () => { | ||
| it('should disconnect the ResizeObserver', () => { | ||
| service.ngOnDestroy(); | ||
| expect(mockDisconnect).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should not remove the sentinel element on destroy', () => { | ||
| service.ngOnDestroy(); | ||
| expect(document.querySelector(`[${SENTINEL_ATTR}]`)).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('should null out internal references after destroy', () => { | ||
| service.ngOnDestroy(); | ||
| expect(() => service.ngOnDestroy()).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('sentinel reuse (microfrontend scenario)', () => { | ||
| it('should reuse the existing sentinel when a second instance is created', () => { | ||
| const service2 = TestBed.runInInjectionContext( | ||
| () => new CpsRootFontSizeService() | ||
| ); | ||
|
|
||
| expect(document.querySelectorAll(`[${SENTINEL_ATTR}]`).length).toBe(1); | ||
|
|
||
| service2.ngOnDestroy(); | ||
| }); | ||
|
|
||
| it('should keep the sentinel alive when the non-owning instance is destroyed', () => { | ||
| const service2 = TestBed.runInInjectionContext( | ||
| () => new CpsRootFontSizeService() | ||
| ); | ||
|
|
||
| service2.ngOnDestroy(); | ||
| expect(document.querySelector(`[${SENTINEL_ATTR}]`)).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('should keep the sentinel alive when the owning (first) instance is destroyed while another is active', () => { | ||
| const service2 = TestBed.runInInjectionContext( | ||
| () => new CpsRootFontSizeService() | ||
| ); | ||
|
|
||
| service.ngOnDestroy(); | ||
| expect(document.querySelector(`[${SENTINEL_ATTR}]`)).not.toBeNull(); | ||
| service2.ngOnDestroy(); | ||
| }); | ||
|
|
||
| it('should keep tracking after the owning instance is destroyed: surviving instance still updates on resize', () => { | ||
| const callbacks: ((entries: unknown[], observer: unknown) => void)[] = []; | ||
| (globalThis as any).ResizeObserver = jest.fn( | ||
| (cb: (entries: unknown[], observer: unknown) => void) => { | ||
| callbacks.push(cb); | ||
| return { observe: mockObserve, disconnect: mockDisconnect }; | ||
| } | ||
| ); | ||
|
|
||
| const service2 = TestBed.runInInjectionContext( | ||
| () => new CpsRootFontSizeService() | ||
| ); | ||
|
|
||
| service.ngOnDestroy(); | ||
|
|
||
| (window.getComputedStyle as jest.Mock).mockReturnValue({ | ||
| fontSize: '20px' | ||
| } as CSSStyleDeclaration); | ||
|
|
||
| callbacks[0]([], null as unknown as ResizeObserver); | ||
|
|
||
| expect(service2.fontSize()).toBe(20); | ||
| service2.ngOnDestroy(); | ||
| }); | ||
|
|
||
| it('should keep the sentinel alive even after all instances are destroyed', () => { | ||
| const service2 = TestBed.runInInjectionContext( | ||
| () => new CpsRootFontSizeService() | ||
| ); | ||
|
|
||
| service.ngOnDestroy(); | ||
| service2.ngOnDestroy(); | ||
|
|
||
| expect(document.querySelector(`[${SENTINEL_ATTR}]`)).not.toBeNull(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('CpsRootFontSizeService (SSR)', () => { | ||
| beforeEach(() => { | ||
| (globalThis as any).ResizeObserver = jest.fn(() => ({ | ||
| observe: jest.fn(), | ||
| disconnect: jest.fn() | ||
| })); | ||
| jest | ||
| .spyOn(window, 'getComputedStyle') | ||
| .mockReturnValue({ fontSize: '16px' } as CSSStyleDeclaration); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| delete (globalThis as any).ResizeObserver; | ||
| }); | ||
|
|
||
| it('should initialize fontSize to 16 in SSR', () => { | ||
| TestBed.configureTestingModule({ | ||
| providers: [{ provide: PLATFORM_ID, useValue: 'server' }] | ||
| }); | ||
| const service = TestBed.inject(CpsRootFontSizeService); | ||
| expect(service.fontSize()).toBe(16); | ||
| }); | ||
|
|
||
| it('should not create a sentinel element in SSR', () => { | ||
| TestBed.configureTestingModule({ | ||
| providers: [{ provide: PLATFORM_ID, useValue: 'server' }] | ||
| }); | ||
| TestBed.inject(CpsRootFontSizeService); | ||
| const doc = TestBed.inject(DOCUMENT); | ||
| expect(doc.querySelector(`[${SENTINEL_ATTR}]`)).toBeNull(); | ||
| }); | ||
|
|
||
| it('should not create a ResizeObserver in SSR', () => { | ||
| const observerCtor = (globalThis as any).ResizeObserver as jest.Mock; | ||
| TestBed.configureTestingModule({ | ||
| providers: [{ provide: PLATFORM_ID, useValue: 'server' }] | ||
| }); | ||
| TestBed.inject(CpsRootFontSizeService); | ||
| expect(observerCtor).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('CPS_ROOT_FONT_SIZE_SERVICE token', () => { | ||
| beforeEach(() => { | ||
| (globalThis as any).ResizeObserver = jest.fn(() => ({ | ||
| observe: jest.fn(), | ||
| disconnect: jest.fn() | ||
| })); | ||
| jest | ||
| .spyOn(window, 'getComputedStyle') | ||
| .mockReturnValue({ fontSize: '16px' } as CSSStyleDeclaration); | ||
|
|
||
| TestBed.configureTestingModule({}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| TestBed.inject(CpsRootFontSizeService).ngOnDestroy(); | ||
| jest.restoreAllMocks(); | ||
| delete (globalThis as any).ResizeObserver; | ||
| TestBed.inject(DOCUMENT) | ||
| .querySelectorAll(`[${SENTINEL_ATTR}]`) | ||
| .forEach((el) => el.remove()); | ||
| }); | ||
|
|
||
| it('should resolve to the CpsRootFontSizeService singleton', () => { | ||
| const token = TestBed.inject(CPS_ROOT_FONT_SIZE_SERVICE); | ||
| const direct = TestBed.inject(CpsRootFontSizeService); | ||
| expect(token).toBe(direct); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.