Skip to content

Commit

Permalink
chore: refactor test for using OnPush strategy (#8890)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Sep 11, 2024
1 parent de77588 commit 09db4f2
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions projects/addon-doc/components/code/tests/highlight.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ChangeDetectionStrategy, type DebugElement} from '@angular/core';
import {ChangeDetectionStrategy, type DebugElement, signal} from '@angular/core';
import {Component, Input} from '@angular/core';
import type {ComponentFixture} from '@angular/core/testing';
import {fakeAsync, TestBed, tick, waitForAsync} from '@angular/core/testing';
Expand All @@ -22,14 +22,13 @@ describe('Highlight Directive', () => {
standalone: true,
imports: [Highlight],
template: `
<code [highlight]="code || ''"></code>
<code [highlight]="code()"></code>
`,
// eslint-disable-next-line @angular-eslint/prefer-on-push-component-change-detection
changeDetection: ChangeDetectionStrategy.Default,
changeDetection: ChangeDetectionStrategy.OnPush,
})
class Test {
@Input()
public code?: string | null;
public code = signal('');
}

beforeEach(waitForAsync(async () => {
Expand Down Expand Up @@ -62,37 +61,39 @@ describe('Highlight Directive', () => {
});

it('should highlight given text', fakeAsync(() => {
component.code = testJsCode;
component.code.set(testJsCode);
fixture.detectChanges();

loader.ready.subscribe((lib: HighlightLibrary) => {
highlightedCode = lib.highlightAuto(
testJsCode,
// note: enable auto detect language mode
null as unknown as string[],
).value;
});

tick(500);

expect(directiveElement.nativeElement.innerHTML).toBe(highlightedCode);
}));

it('should reset text if empty string was passed', () => {
component.code = '';
component.code.set('');
fixture.detectChanges();

expect(directiveElement.nativeElement.innerHTML).toBe('');
});

it('should not highlight if code is undefined', () => {
jest.spyOn(directiveInstance, 'highlightElement');
component.code = null;
component.code.set('');
fixture.detectChanges();

expect(directiveInstance.highlightElement).not.toHaveBeenCalled();
});

it('should highlight given text and highlight another text when change', fakeAsync(() => {
component.code = testJsCode;
component.code.set(testJsCode);
fixture.detectChanges();
loader.ready.subscribe((lib: HighlightLibrary) => {
highlightedCode = lib.highlightAuto(
Expand All @@ -105,7 +106,7 @@ describe('Highlight Directive', () => {

expect(directiveElement.nativeElement.innerHTML).toBe(highlightedCode);

component.code = testHtmlCode;
component.code.set(testHtmlCode);
fixture.detectChanges();
loader.ready.subscribe((lib: HighlightLibrary) => {
highlightedCode = lib.highlightAuto(
Expand All @@ -118,13 +119,13 @@ describe('Highlight Directive', () => {

expect(directiveElement.nativeElement.innerHTML).toBe(highlightedCode);

component.code = '';
component.code.set('');
fixture.detectChanges();
tick(300);

expect(directiveElement.nativeElement.innerHTML).toBe('');

component.code = null;
component.code.set('');
fixture.detectChanges();
tick(300);

Expand Down

0 comments on commit 09db4f2

Please sign in to comment.