mirror of
https://github.com/angular/angular.git
synced 2026-11-23 07:05:14 -05:00
The copy button read the whole block's `innerText` and then removed every
`\n\n\n` sequence, which dropped the blank lines between lines of code.
On blocks with line numbers, the same `innerText` also picked up the
number column, so pasting produced "1", the first line, "2", "3" and so
on.
Join the text of the visible `.line` elements instead. Line numbers sit
outside those elements, and lines hidden by an example's visible range
stay hidden, so the clipboard matches what the block shows.
The `.remove` branch goes with it, since no line has carried that class
since 8ec0a4ee5c dropped diff support from docs-code.
136 lines
4.2 KiB
TypeScript
136 lines
4.2 KiB
TypeScript
/*!
|
|
* @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';
|
|
|
|
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;');
|
|
});
|
|
|
|
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');
|
|
|
|
await fixture.whenStable();
|
|
|
|
const button = fixture.debugElement.query(By.directive(CopySourceCodeButton)).nativeElement;
|
|
button.click();
|
|
await fixture.whenStable();
|
|
|
|
expect(button).toHaveClass(SUCCESSFULLY_COPY_CLASS_NAME);
|
|
|
|
clock.tick(CONFIRMATION_DISPLAY_TIME_MS);
|
|
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');
|
|
|
|
await fixture.whenStable();
|
|
|
|
const button = fixture.debugElement.query(By.directive(CopySourceCodeButton)).nativeElement;
|
|
button.click();
|
|
|
|
await fixture.whenStable();
|
|
|
|
expect(button).toHaveClass(FAILED_COPY_CLASS_NAME);
|
|
|
|
clock.tick(CONFIRMATION_DISPLAY_TIME_MS);
|
|
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('');
|
|
}
|