Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

136 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

/*!
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {ComponentFixture, TestBed} from '@angular/core/testing';
2026-03-25 12:19:57 +01:00
import {Clipboard} from '@angular/cdk/clipboard';
import {Component, signal} from '@angular/core';
import {By} from '@angular/platform-browser';
import {
CONFIRMATION_DISPLAY_TIME_MS,
CopySourceCodeButton,
} from './copy-source-code-button.component';
const SUCCESSFULLY_COPY_CLASS_NAME = 'docs-copy-source-code-button-success';
const FAILED_COPY_CLASS_NAME = 'docs-copy-source-code-button-failed';
describe('CopySourceCodeButton', () => {
let component: CodeSnippetWrapper;
let fixture: ComponentFixture<CodeSnippetWrapper>;
let copySpy: jasmine.Spy<(text: string) => boolean>;
beforeEach(async () => {
TestBed.configureTestingModule({
imports: [CodeSnippetWrapper],
});
fixture = TestBed.createComponent(CodeSnippetWrapper);
component = fixture.componentInstance;
await fixture.whenStable();
});
beforeEach(() => {
const clipboardService = TestBed.inject(Clipboard);
copySpy = spyOn(clipboardService, 'copy');
});
it('should call clipboard service when clicked on copy source code', async () => {
const expectedCodeToBeCopied = 'npm install -g @angular/cli';
component.code.set(expectedCodeToBeCopied);
await fixture.whenStable();
const button = fixture.debugElement.query(By.directive(CopySourceCodeButton)).nativeElement;
button.click();
expect(copySpy.calls.argsFor(0)[0].trim()).toBe(expectedCodeToBeCopied);
});
it('should keep blank lines and skip line numbers', async () => {
component.code.set(
'<span class="shiki-ln-number">1</span><span class="line">const a = 1;</span>\n' +
'<span class="shiki-ln-number">2</span><span class="line"></span>\n' +
'<span class="shiki-ln-number">3</span><span class="line">const b = 2;</span>',
);
await fixture.whenStable();
const button = fixture.debugElement.query(By.directive(CopySourceCodeButton)).nativeElement;
button.click();
expect(copySpy.calls.argsFor(0)[0]).toBe('const a = 1;\n\nconst b = 2;');
});
it('should skip hidden lines', async () => {
component.code.set(
'<span class="line">const a = 1;</span>\n' +
'<span class="line hidden">const b = 2;</span>\n' +
'<span class="line">const c = 3;</span>',
);
await fixture.whenStable();
const button = fixture.debugElement.query(By.directive(CopySourceCodeButton)).nativeElement;
button.click();
expect(copySpy.calls.argsFor(0)[0]).toBe('const a = 1;\nconst c = 3;');
});
2026-01-04 05:23:19 +01:00
it(`should set ${SUCCESSFULLY_COPY_CLASS_NAME} for ${CONFIRMATION_DISPLAY_TIME_MS} ms when copy was executed properly`, async () => {
const clock = jasmine.clock().install();
component.code.set('example');
2026-01-04 05:23:19 +01:00
await fixture.whenStable();
const button = fixture.debugElement.query(By.directive(CopySourceCodeButton)).nativeElement;
button.click();
2026-01-04 05:23:19 +01:00
await fixture.whenStable();
expect(button).toHaveClass(SUCCESSFULLY_COPY_CLASS_NAME);
clock.tick(CONFIRMATION_DISPLAY_TIME_MS);
2026-01-04 05:23:19 +01:00
await fixture.whenStable();
expect(button).not.toHaveClass(SUCCESSFULLY_COPY_CLASS_NAME);
clock.uninstall();
});
it(`should set ${FAILED_COPY_CLASS_NAME} for ${CONFIRMATION_DISPLAY_TIME_MS} ms when copy failed`, async () => {
const clock = jasmine.clock().install();
component.code.set('example');
copySpy.and.throwError('Fake copy error');
2026-01-04 05:23:19 +01:00
await fixture.whenStable();
const button = fixture.debugElement.query(By.directive(CopySourceCodeButton)).nativeElement;
button.click();
2026-01-04 05:23:19 +01:00
await fixture.whenStable();
expect(button).toHaveClass(FAILED_COPY_CLASS_NAME);
clock.tick(CONFIRMATION_DISPLAY_TIME_MS);
2026-01-04 05:23:19 +01:00
await fixture.whenStable();
expect(button).not.toHaveClass(FAILED_COPY_CLASS_NAME);
clock.uninstall();
});
});
@Component({
template: `
<pre>
<code [innerHtml]="code()"></code>
</pre>
<button docs-copy-source-code></button>
`,
imports: [CopySourceCodeButton],
})
class CodeSnippetWrapper {
code = signal('');
}