@lunit/use-control-log
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Sample Codes

https://lunit-io.github.io/opt-tool-frontend

Stories

Tests

__tests__/useControlLog.test.ts

import { act, renderHook } from '@testing-library/react-hooks';
import { useControlLog } from '../useControlLog';

//tslint:disable:react-hooks-nesting
describe('useControlLog()', () => {
  test('addControlLog', () => {
    const {result, rerender} = renderHook(({sessionId}) => useControlLog(sessionId), {
      initialProps: {
        sessionId: 'session1',
      },
    });
    
    expect(result.current.controlLog.current).toHaveLength(0);
    
    act(() => {
      result.current.addControlLog({command: 'a'});
      result.current.addControlLog({command: 'b'});
    });
    
    expect(result.current.controlLog.current).toHaveLength(2);
    
    act(() => {
      result.current.addControlLog({command: 'c'});
      result.current.addControlLog({command: 'd'});
    });
    
    expect(result.current.controlLog.current).toHaveLength(4);
    expect(result.current.controlLog.current!.map(({command}) => command).join('')).toBe('abcd');
    
    rerender({
      sessionId: 'session2',
    });
    
    expect(result.current.controlLog.current).toHaveLength(0);
  });
});

__tests__/useControlLog.test.ts

import { act, renderHook } from '@testing-library/react-hooks';
import { useControlLog } from '../useControlLog';

//tslint:disable:react-hooks-nesting
describe('useControlLog()', () => {
  test('addControlLog', () => {
    const {result, rerender} = renderHook(({sessionId}) => useControlLog(sessionId), {
      initialProps: {
        sessionId: 'session1',
      },
    });
    
    expect(result.current.controlLog.current).toHaveLength(0);
    
    act(() => {
      result.current.addControlLog({command: 'a'});
      result.current.addControlLog({command: 'b'});
    });
    
    expect(result.current.controlLog.current).toHaveLength(2);
    
    act(() => {
      result.current.addControlLog({command: 'c'});
      result.current.addControlLog({command: 'd'});
    });
    
    expect(result.current.controlLog.current).toHaveLength(4);
    expect(result.current.controlLog.current!.map(({command}) => command).join('')).toBe('abcd');
    
    rerender({
      sessionId: 'session2',
    });
    
    expect(result.current.controlLog.current).toHaveLength(0);
  });
});

Readme

Keywords

none

Package Sidebar

Install

npm i @lunit/use-control-log

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

10.1 kB

Total Files

6

Last publish

Collaborators

  • takhyunkim
  • deminoth
  • jyseo