开玩笑地测试两个不同的功能

时间:2019-11-08 23:49:54

标签: javascript reactjs testing redux jestjs

我是测试世界的新手,我希望得到您的帮助。 我有两个不同的功能。

第一个与每秒位数的转换器有关,以使其更具人类可读性

const formatBitrate = bitrate => {
  if (bitrate === 0) {
    return '0 Bytes';
  }

  const k = 1024;
  const sizes = ['Bytes', 'kbps', 'mbps', 'gbps', 'tbps', 'pbps', 'ebps', 'zbps', 'ybps'];
  const i = Math.floor(Math.log(bitrate) / Math.log(k));

  return `${parseFloat((bitrate / k ** i).toFixed(0))}\u00A0${sizes[i]}`;
};

第二个是在mapStateToProps函数中,在那里我借助map函数并使用一些Math预先构建的函数来构建和创建新的对象数组。

const mapStateToProps = state => {
  let plotData = state.qualityResults.plotData;
  const lastTimestamp = Number(Object.keys(plotData)[0]);
  plotData = Object.keys(plotData).map(key => ({
    timestamp: Number(key),
    bytesReceived: plotData[key].bytesReceived,
    seconds: `${Math.ceil(Math.abs(Number(key) - lastTimestamp) / 1000)}s`
  }));

  return {
    plotData,
    mos: Number(state.qualityResults.video.mos).toFixed(1),
    packetLossRatio: Number(state.qualityResults.video.packetLossRatio).toFixed(0),
    bitrate: formatBitrate(state.qualityResults.video.bitrate)
  };
};

1 个答案:

答案 0 :(得分:0)

您应该测试并确保formatBitrate函数的逻辑正确。然后,您可以测试mapStateToProps函数的逻辑。

例如

index.ts

export const formatBitrate = bitrate => {
  if (bitrate === 0) {
    return '0 Bytes';
  }

  const k = 1024;
  const sizes = ['Bytes', 'kbps', 'mbps', 'gbps', 'tbps', 'pbps', 'ebps', 'zbps', 'ybps'];
  const i = Math.floor(Math.log(bitrate) / Math.log(k));

  return `${parseFloat((bitrate / k ** i).toFixed(0))}\u00A0${sizes[i]}`;
};

export const mapStateToProps = state => {
  let plotData = state.qualityResults.plotData;
  const lastTimestamp = Number(Object.keys(plotData)[0]);
  plotData = Object.keys(plotData).map(key => ({
    timestamp: Number(key),
    bytesReceived: plotData[key].bytesReceived,
    seconds: `${Math.ceil(Math.abs(Number(key) - lastTimestamp) / 1000)}s`
  }));

  return {
    plotData,
    mos: Number(state.qualityResults.video.mos).toFixed(1),
    packetLossRatio: Number(state.qualityResults.video.packetLossRatio).toFixed(0),
    bitrate: formatBitrate(state.qualityResults.video.bitrate)
  };
};

index.spec.ts

import { formatBitrate, mapStateToProps } from './';

describe('formatBitrate', () => {
  test('should format bitrate', () => {
    const actualValue = formatBitrate(1024);
    expect(actualValue).toBe('1\u00A0kbps');
  });
  test('should return 0 Bytes', () => {
    const actualValue = formatBitrate(0);
    expect(actualValue).toBe('0 Bytes');
  });
});

describe('mapStateToProps', () => {
  test('should return mapped state', () => {
    const mState = {
      qualityResults: {
        video: {
          mos: 2,
          packetLossRatio: 1,
          bitrate: 1024
        },
        plotData: {
          2333333: {
            bytesReceived: 666
          }
        }
      }
    };
    const actualValue = mapStateToProps(mState);
    expect(actualValue).toEqual({
      plotData: [{ timestamp: 2333333, bytesReceived: 666, seconds: '0s' }],
      mos: '2.0',
      packetLossRatio: '1',
      bitrate: '1\u00A0kbps'
    });
  });
});

覆盖率100%的单元测试结果:

 PASS  src/stackoverflow/58775089/index.spec.ts (7.933s)
  formatBitrate
    ✓ should format bitrate (4ms)
    ✓ should return 0 Bytes (1ms)
  mapStateToProps
    ✓ should return mapped state (2ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.ts |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        9.301s, estimated 11s

源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/58775089