client/src/components/__tests__/NoteInput.test.js
author ymh <ymh.work@gmail.com>
Tue, 20 Jun 2017 19:05:01 +0200
changeset 66 f402435be429
parent 14 df6780e48eb5
child 168 ea92f4fe783d
permissions -rw-r--r--
Action types cleaning
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import React from 'react';
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import { shallow, mount } from 'enzyme';
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import NoteInput from '../NoteInput';
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
     4
import { Plain } from 'slate'
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
const setup = (propOverrides, doMount=false) => {
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
  const props = Object.assign({
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
      addNote: jest.fn()
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
  }, propOverrides);
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    11
  const renderFn = doMount?mount:shallow;
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
  const wrapper = renderFn(<NoteInput {...props} />);
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
  return {
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    props,
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    wrapper,
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
  }
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
};
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    20
// Element.focus() doesn't work
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    21
// @see https://stackoverflow.com/questions/42213522/mocking-document-createrange-for-jest
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    22
beforeAll(() => {
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    23
  window.getSelection = () => {
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    24
    return {
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    25
      removeAllRanges: () => {},
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    26
      addRange: () => {}
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    27
    };
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    28
  };
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    29
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    30
  window.Range = function Range() {};
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    31
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    32
  const createContextualFragment = (html) => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    33
    const div = document.createElement('div');
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    34
    div.innerHTML = html;
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    35
    return div.children[0]; // so hokey it's not even funny
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    36
  };
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    37
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    38
  Range.prototype.createContextualFragment = (html) => createContextualFragment(html);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    39
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    40
  // HACK: Polyfil that allows codemirror to render in a JSDOM env.
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    41
  window.document.createRange = function createRange() {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    42
    return {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    43
      setEnd: () => {},
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    44
      setStart: () => {},
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    45
      getBoundingClientRect: () => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    46
        return { right: 0 };
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    47
      },
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    48
      compareBoundaryPoints: () => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    49
        return 0;
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    50
      },
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    51
      getClientRects: () => [],
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    52
      createContextualFragment,
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    53
    };
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    54
  };
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    55
});
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    56
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
describe('Notes container Component', () => {
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
  test('render', () => {
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    const { wrapper } = setup();
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    expect(wrapper.exists()).toBe(true)
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
  });
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    63
  test('button is disabled when there is no text', () => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    64
    const { props, wrapper } = setup({}, true);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    65
    const button = wrapper.find('button');
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    66
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    67
    expect(button.prop('disabled')).toBe(true);
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
  });
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    70
  test('button is not disabled when there is text', () => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    71
    const { props, wrapper } = setup({}, true);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    72
    const button = wrapper.find('button');
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    73
    const editor = wrapper.find('SlateEditor').find('Editor').node;
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    74
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    75
    // FIXME simulate('change') doesn't work
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    76
    editor.onChange(Plain.deserialize('Hello world'));
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    77
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    78
    expect(button.prop('disabled')).toBe(false);
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
  });
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    81
  // test('click button', () => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    82
  //     const { props, wrapper } = setup({}, true);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    83
  //     wrapper.find('button').simulate('click');
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    84
  //     expect(props.addNote.mock.calls.length).toBe(1);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    85
  // });
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    86
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    87
  // test('note value on clickbutton', () => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    88
  //     const { props, wrapper } = setup({}, true);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    89
  //     // This does nothing... must find a way to make it work
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    90
  //     wrapper.find('SlateEditor').simulate('change', {target: {value: 'note text'}});
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    91
  //     wrapper.find('button').simulate('click');
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    92
  //     expect(props.addNote.mock.calls.length).toBe(1);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    93
  //     // cf. previous comment
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    94
  //     //expect(props.addNote.mock.calls[0]).toEqual(['note text']);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    95
  // });
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    96
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
});
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98