从assets文件夹加载xls文件(Angular6)

时间:2018-06-15 10:23:32

标签: httpclient angular6

我想从项目的assets文件夹中加载一个xls文件,将其提交给单元测试。我创建了一个方法,应该允许导入此文件,但它不起作用。有人可以帮帮我吗? 这是我的服务代码:

export class FileLoaderService {
  constructor(private http: HttpClient) {

    openMyFile(filePath: string): Observable<Object> {
        return this.http.get(filePath);
    }


    // Here I want to onvert Excel to JSON
    getWorkbook (fileReader: FileReader): any {
        const arrayBuffer = fileReader.result;
        const data = new Uint8Array(arrayBuffer);
        const arr = new Array();
        for (let i = 0; i !== data.length; ++i) {
          arr[i] = String.fromCharCode(data[i]);
        }
        const bstr = arr.join('');
        return XLSX.read(bstr, {type: 'binary'});
    }
}

我的测试组件:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { FileLoaderPageComponent } from './file-loader-page.component';
import {FileLoaderService} from '../../services/FileLoader/file-loader.service';
import {FileLoaderModule} from '../../file-loader.module';

describe('FileLoaderPageComponent', () => {
  let component: FileLoaderPageComponent;
  let fixture: ComponentFixture<FileLoaderPageComponent>;
  let fileLoaderService: FileLoaderService;
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports : [FileLoaderModule]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(FileLoaderPageComponent);
    fileLoaderService = TestBed.get(FileLoaderService);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  fit ('should be OK', () => {
    expect(true).toBeTruthy();
  });

  fit('Should load excel file and read content', (done) => {

    fileLoaderService.openMyFile('./assets/Tests/Files/excelFile.xlsx').subscribe(data => {
      console.log(data);
      const monExcel = fileLoaderService.readMyFile(data);
      fileLoaderService
      done();
    });
  });
});

0 个答案:

没有答案