JEST: Как протестировать добавление данных к объекту - PullRequest
0 голосов
/ 27 мая 2020

Я новичок в reactjs, js и Jest. Пожалуйста, извините, если это очень простой c вопрос.

Я хочу написать модульный тест для нижеприведенного. Кто-нибудь может мне помочь, пожалуйста. Считайте меня начинающим шутить.

class xyz {
  storingRepo: Object;

  constructor() {
    this.storingRepo = {};
  }

  storeProduct(a: string, b: string) {
    const key = `${a}_${version}`;
    this.waitingProductsRepo[key] = true;
  }
}

export default new xyz();

1 Ответ

1 голос
/ 28 мая 2020

Вот решение для модульного теста:

xyz.ts:

class xyz {
  storingRepo: Object;

  constructor() {
    this.storingRepo = {};
  }

  storeProduct(a: string, b: string) {
    const key = `${a}_${b}`;
    this.storingRepo[key] = true;
  }
}

export default new xyz();

xyz.test.ts:

import xyz from './xyz';

describe('62045858', () => {
  it('should pass', () => {
    expect(Object.keys(xyz.storingRepo)).toHaveLength(0);
    xyz.storeProduct('a', 'b');
    expect(xyz.storingRepo).toHaveProperty('a_b');
    expect(xyz.storingRepo['a_b']).toBeTruthy();
  });
});

Результат теста с покрытием отчет:

 PASS  stackoverflow/62045858/xyz.test.ts (9.997s)
  62045858
    ✓ should pass (3ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 xyz.ts   |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        11.815s
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...