测试复制到剪贴板功能

时间:2020-02-21 11:27:57

标签: angular angular-material angular-test

我现在正在学习Angular,我在名为dropdown.component.ts的组件中有一个函数,可将单击的文本复制到剪贴板。该函数本身可以工作,但是我现在想为其编写规格测试。该函数的外观如下:

    copyText(text: string) {
        const copy = (e : ClipboardEvent) => {
            e.clipboardData.setData('text/plain', text);
            e.preventDefault();
        };
        document.addEventListener('copy', copy);
        document.execCommand('copy');
        document.removeEventListener('copy', copy);
    }

我不确定如何进行测试。本质上,我想测试单击copy按钮时,文本是否成功复制到剪贴板。

这是我到目前为止所拥有的:

describe('DropdownComponent', () => {
    let component: DropdownComponent;
    let fixture: ComponentFixture<TreeModalComponent>;

    beforeEach(async () => {
        TestBed.configureTestingModule({
            imports: [
                MatMenuModule,
                MatTreeModule,
                MatSnackBarModule,
            ],
            declarations: [
                TreeModalComponent,
            ],
            providers: [
                {
                    provide: MatDialog,
                    useClass: MatDialogMock
                },
                { provide: MatDialogRef, useValue: {} },
                { provide: MAT_DIALOG_DATA, useValue: { properties: testFTSResultData[0].properties } },
                HttpClient,
                HttpHandler,
            ],
            schemas: [NO_ERRORS_SCHEMA],
        }).compileComponents();
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(TreeModalComponent);
        component = fixture.componentInstance;
        component.properties = testResultData[0].properties;
        dialog = TestBed.get(MatDialogRef);
        viewport.set('laptop');
        fixture.detectChanges();
    });

从这里我要添加特定的测试:

    //test copy to clipboard
    it('should copy text to clipboard', () => {
        component.copyText('copy text')
    });

我不确定随后的行应该是什么,以确保剪贴板可以访问字符串“复制文本”。有人可以建议吗?

0 个答案:

没有答案