Skip to content

Commit

Permalink
fix: missing success msg (#300)
Browse files Browse the repository at this point in the history
* Fix test 'should push copy response to copySubject' -> we must subscribe before calling click() on button, or we miss the event and no checks are executed

* Response published by handleResult() do not depend on use of cbOnSuccess
  • Loading branch information
MathieuCoupe authored Mar 11, 2022
1 parent c0868e2 commit 5fe4457
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
49 changes: 47 additions & 2 deletions projects/ngx-clipboard/src/lib/ngx-clipboard.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { ClipboardService } from './ngx-clipboard.service';
template: ` <span>PlaceHolder HTML to be Replaced</span> `
})
export class TestClipboardComponent {
public text = 'test';
public text = 'text';
public isCopied: boolean;
public copySuccessMsg = 'Foo bar';
public copySuccessMsg = 'copySuccessMsg';
}

/**
Expand Down Expand Up @@ -120,7 +120,51 @@ describe('Directive: clipboard', () => {
it(
'should push copy response to copySubject',
waitForAsync(() => {
spy.and.returnValue(true);
const component = fixture.componentInstance;
clipboardService.copyResponse$.subscribe((res: IClipboardResponse) => {
expect(res).toBeDefined();
expect(res.isSuccess).toEqual(true);
expect(res.content).toEqual(component.text);
expect(res.successMessage).toEqual(component.copySuccessMsg);
expect(res.event).toBeDefined();
});
button.click();
})
);
});

describe('copy when cbOnSuccess is not set', () => {
let template: string;
let fixture: ComponentFixture<TestClipboardComponent>;
let clipboardService: ClipboardService;
let spy: jasmine.Spy;
let button: HTMLButtonElement;
beforeEach(() => {
template = `<button ngxClipboard [cbContent]="'text'" [cbSuccessMsg]="copySuccessMsg">copy</button>`;
fixture = createTestComponent(template);
clipboardService = fixture.debugElement.injector.get(ClipboardService);
// Setup spy on the `copyText` method, somehow document.execCommand('copy') doesn't work in Karma
spy = spyOn(clipboardService, 'copyText' as keyof ClipboardService);
fixture.detectChanges();
button = fixture.debugElement.nativeElement.querySelector('button');
});

it(
'should not fire cbOnSuccess after copy successfully',
waitForAsync(() => {
spy.and.returnValue(true);
button.click();
fixture.whenStable().then(() => {
expect(fixture.componentInstance.isCopied).toBeFalsy();
});
})
);

it(
'should push copy response to copySubject',
waitForAsync(() => {
spy.and.returnValue(true);
const component = fixture.componentInstance;
clipboardService.copyResponse$.subscribe((res: IClipboardResponse) => {
expect(res).toBeDefined();
Expand All @@ -129,6 +173,7 @@ describe('Directive: clipboard', () => {
expect(res.successMessage).toEqual(component.copySuccessMsg);
expect(res.event).toBeDefined();
});
button.click();
})
);
});
Expand Down
6 changes: 2 additions & 4 deletions projects/ngx-clipboard/src/lib/ngx-clipboard.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,13 @@ export class ClipboardDirective implements OnInit, OnDestroy {
private handleResult(succeeded: boolean, copiedContent: string | undefined, event: MouseEvent): void {
let response: IClipboardResponse = {
isSuccess: succeeded,
content: copiedContent,
successMessage: this.cbSuccessMsg,
event
};

if (succeeded) {
if (this.cbOnSuccess.observers.length > 0) {
response = Object.assign(response, {
content: copiedContent,
successMessage: this.cbSuccessMsg
});
this.ngZone.run(() => {
this.cbOnSuccess.emit(response);
});
Expand Down

0 comments on commit 5fe4457

Please sign in to comment.