client/src/components/__tests__/NoteInput.test.js
author ymh <ymh.work@gmail.com>
Fri, 30 Nov 2018 10:53:15 +0100
changeset 183 f8f3af9e5c83
parent 168 ea92f4fe783d
permissions -rw-r--r--
Change the settings to avoid using Session authentication for rest framework as it raise exceptions in case client and backend are on the same domain On the filter, adapt to take into account new version of django_filters
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
     1
// import React from 'react';
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
     2
// import { shallow, mount } from 'enzyme';
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
     3
// import NoteInput from '../NoteInput';
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
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
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
     6
// const setup = (propOverrides, doMount=false) => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
     7
//   const props = Object.assign({
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
     8
//       addNote: jest.fn(),
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
     9
//       session: { _id: 'abcd' },
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    10
//       autoSubmit: false,
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    11
//       setAutoSubmit: jest.fn(),
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    12
//       annotationCategories: []
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    13
//   }, propOverrides);
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    15
//   const renderFn = doMount?mount:shallow;
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    16
//   const wrapper = renderFn(<NoteInput {...props} />);
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    18
//   return {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    19
//     props,
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    20
//     wrapper,
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    21
//   }
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    22
// };
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    24
// Element.focus() doesn't work
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    25
// @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
    26
beforeAll(() => {
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    27
  window.getSelection = () => {
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    28
    return {
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    29
      removeAllRanges: () => {},
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    30
      addRange: () => {}
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
  };
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    33
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    34
  window.Range = function Range() {};
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    35
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    36
  const createContextualFragment = (html) => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    37
    const div = document.createElement('div');
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    38
    div.innerHTML = html;
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    39
    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
    40
  };
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    41
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    42
  Range.prototype.createContextualFragment = (html) => createContextualFragment(html);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    43
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    44
  // 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
    45
  window.document.createRange = function createRange() {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    46
    return {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    47
      setEnd: () => {},
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    48
      setStart: () => {},
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    49
      getBoundingClientRect: () => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    50
        return { right: 0 };
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    51
      },
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    52
      compareBoundaryPoints: () => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    53
        return 0;
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    54
      },
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    55
      getClientRects: () => [],
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    56
      createContextualFragment,
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    57
    };
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    58
  };
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    59
});
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    60
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
describe('Notes container Component', () => {
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
  test('render', () => {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    63
    //pass
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
  });
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    65
  // test('render', () => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    66
  //   const { wrapper } = setup({}, true);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    67
  //   expect(wrapper.exists()).toBe(true)
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    68
  // });
3
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
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    71
  // test('button is disabled when there is no text', () => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    72
  //   const { props, wrapper } = setup({}, true);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    73
  //   const button = wrapper.find('button');
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    75
  //   expect(button.prop('disabled')).toBe(true);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    76
  // });
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    77
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    78
  // test('button is not disabled when there is text', () => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    79
  //   const { props, wrapper } = setup({}, true);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    80
  //   const button = wrapper.find('button');
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    81
  //   const editor = wrapper.find('SlateEditor').find('Editor').node;
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    82
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    83
  //   // FIXME simulate('change') doesn't work
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    84
  //   editor.onChange(Plain.deserialize('Hello world'));
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    85
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    86
  //   expect(button.prop('disabled')).toBe(false);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 14
diff changeset
    87
  // });
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
14
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    89
  // test('click button', () => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    90
  //     const { props, wrapper } = setup({}, true);
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
  // });
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    94
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    95
  // test('note value on clickbutton', () => {
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    96
  //     const { props, wrapper } = setup({}, true);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    97
  //     // 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
    98
  //     wrapper.find('SlateEditor').simulate('change', {target: {value: 'note text'}});
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    99
  //     wrapper.find('button').simulate('click');
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   100
  //     expect(props.addNote.mock.calls.length).toBe(1);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   101
  //     // cf. previous comment
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   102
  //     //expect(props.addNote.mock.calls[0]).toEqual(['note text']);
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   103
  // });
df6780e48eb5 Fix all tests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   104
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
});
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106