Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
fix(copy-to-clipboard): Fixes an issue that the copy to clipboard was…
Browse files Browse the repository at this point in the history
… called on initial render.
  • Loading branch information
ffriedl89 committed Jan 12, 2022
1 parent b46f305 commit 48eb87d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class="dt-copy-to-clipboard-btn-button"
[variant]="variant"
#copyButton
[cdkCopyToClipboard]="copyToClipboard()"
[cdkCopyToClipboard]="_inputValue"
(cdkCopyToClipboardCopied)="_copiedToClipboard()"
>
<dt-icon *ngIf="showIcon" name="checkmark"></dt-icon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { DtInputModule } from '@dynatrace/barista-components/input';
import { createComponent } from '@dynatrace/testing/browser';

describe('DtCopyToClipboard', () => {
const execCommandMock = jest.fn().mockReturnValue(true);

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -44,10 +46,15 @@ describe('DtCopyToClipboard', () => {
});
TestBed.compileComponents();
// tslint:disable-next-line:no-any
document.execCommand = (): boolean => true;
document.execCommand = execCommandMock;
}),
);

it('should not trigger a copy action on render', (): void => {
createComponent(CallbackBehaviorTestApp);
expect(execCommandMock).not.toHaveBeenCalled();
});

it('should trigger callback - at least 1 copy must be called', (): void => {
const fixture = createComponent(CallbackBehaviorTestApp);
const buttonDebugElement = fixture.debugElement.query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,14 @@ export class DtCopyToClipboard implements AfterContentInit, OnDestroy {

private _timer: Subscription;

/** @internal returns the current value from the projected input */
get _inputValue(): string | undefined {
return this._input?.nativeElement.value;
}

/** Copies the provided content to the clipboard. */
copyToClipboard(): void {
const value = this._input.nativeElement.value;
const value = this._inputValue;
if (this._input && isDefined(value)) {
this._cdkClipboard.copy(value);
} else {
Expand Down

0 comments on commit 48eb87d

Please sign in to comment.