jeudi 23 août 2018

How to test that I have loaded my JSON - Angular2+

I have an Angular method that simply loads the contents of a locally stored JSON file featuring an array, however I cannot seem to test it.

test.ts (shortened for conciseness)

describe('MyComponent', () => {

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [MyComponent],
      imports: [HttpClientTestingModule],
    });

    fixture = TestBed.createComponent(EventComponent);
    component = fixture.componentInstance;

    mockEventService.getById.and.returnValue(of(event));
    component.ngOnInit();

  it('should load status data from local json', () => {
    const data = require('../../../assets/myData.json');
    component.getRaceStatuses();
    expect(component.raceStatuses).toEqual(data);
  });
}

MyComponent.ts

data: string[];

constructor(private httpClient: HttpClient) {}

ngOnInit() {
   this.getRaceStatuses().subscribe(data =
       this.data = data;
   }
}

getData(): Observable<any> {
    const data = '../../../assets/data.json';
    return this.httpClient.get(data);
  }

data.json

[
  {
    "name": "dormant",
    "id": 1,
    "validToId": [2]
  },
  {
    "name": "parading",
    "id": 2,
    "validToId": [3]
  }
]

Aucun commentaire:

Enregistrer un commentaire