aboutsummaryrefslogtreecommitdiffstats
path: root/vaadin-grid/test/grid-editor-handler.html
blob: 20c6e660d8cfd4185976c5d0492d7b166a113836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <script src="../../webcomponentsjs/webcomponents-lite.min.js"></script>
  <script src="../../web-component-tester/browser.js"></script>

  <script src="common.js"></script>

  <link rel="import" href="../vaadin-grid.html">
</head>

<body>

<div id="gridwrapper"></div>

<script>
  describe.feature('using editor handler', function() {
    var editorHandler;

    beforeEach(function() {
      grid.columns = [{name: 'Name'}, {name: 'Value'}];
      grid.editor.cancel();

      editorHandler = {
        getCellEditor: function(columnObject) {
          var input = document.createElement("input");
          return input;
        },
        bind: function(req) {
          for (var i = 0; i < req.grid.columns.length; i++) {
            var col = req.grid.columns[i];
            if (!col.readOnly) {
              var el = req.getCellEditor(col);
              el.value = req.dataItem[i];
            }
          }
          req.success();
        },
        save: function(req) {
          var failingColumns = [];
          for (var i = 0; i < req.grid.columns.length; i++) {
            var col = req.grid.columns[i];
            var el = req.getCellEditor(col);
            if (el.value == "fail") {
              failingColumns.push(col);
            }
          }
          if (failingColumns.length > 0) {
            req.failure("Save failed", failingColumns);
          } else {
            req.success();
          }
        }
      };

      grid.editor.handler = editorHandler;

      return grid;
    });

    it('editor: enabled', function() {
      grid.columns[0].name = "name";
      grid.columns[1].name = "value";
      grid.editor.handler = editorHandler;
      expect(grid.editor.editRow.bind(grid.editor, 0)).to.throw("editor is not enabled");
      grid.editor.enabled = true;
      grid.editor.editRow(0);

      assert.isTrue(gridContainsText(grid, "v-grid-editor"));
    });

    it('editor: saveButtonText and cancelButtonText', function() {
      grid.columns[0].name = "name";
      grid.columns[1].name = "value";
      grid.editor.handler = editorHandler;
      grid.editor.enabled = true;
      grid.editor.editRow(0);
      grid.editor.saveButtonText = "saveButtonTextFoo";
      grid.editor.cancelButtonText = "cancelButtonTextFoo";

      assert.isTrue(gridContainsText(grid, "saveButtonTextFoo"));
      assert.isTrue(gridContainsText(grid, "cancelButtonTextFoo"));
    });

    it('editor: editrow', function() {
      grid.columns[0].name = "name";
      grid.columns[1].name = "value";
      grid.editor.handler = editorHandler;
      grid.editor.enabled = true;

      grid.editor.editRow(0);
      assert.equal(qLocal(".v-grid-editor input").value, "Grid");
      grid.editor.cancel();
      assert.isFalse(gridContainsText(grid, "v-grid-editor"));
      grid.editor.editRow(1);
      assert.equal(qLocal(".v-grid-editor input").value, "VaadinX");
      grid.editor.save();
      assert.isFalse(gridContainsText(grid, "v-grid-editor"));

      expect(grid.editor.editRow.bind(grid.editor, 10)).to.throw("Row index");
    });

    it('editor: failure', function() {
      grid.columns[0].name = "name";
      grid.columns[1].name = "value";
      grid.editor.handler = editorHandler;
      grid.editor.enabled = true;

      grid.editor.editRow(0);
      var fields = qaLocal(".v-grid-editor input");
      fields[0].value = "fail";
      grid.editor.save();

      assert.isTrue(gridContainsText(grid, "Save failed"));
      var cells = qaLocal(".v-grid-editor-cells > div");
      assert.isTrue(cells[0].classList.contains("error"));
      assert.isFalse(cells[1].classList.contains("error"));

    });

    it('editor: return null cell editor', function() {
      grid.columns[0].name = "name";
      grid.columns[1].name = "value";
      grid.editor.handler = {
        getCellEditor: function(columnObject) {
          return columnObject === grid.columns[0] ? null : undefined;
        },
        bind: function(req) {
          req.success();
        }
      };
      grid.editor.enabled = true;

      grid.editor.editRow(0);
      var cells = qaLocal(".v-grid-editor .v-grid-editor-cells > div");
      assert.lengthOf(cells, 2);

      var editors = qaLocal(".v-grid-editor .v-grid-editor-cells > div *");
      assert.lengthOf(editors, 0);
    });

    it('editor: element instance after save', function() {
      grid.editor.enabled = true;
      grid.editor.handler = editorHandler;

      var editor1, editor2;
      editorHandler.save = function(req) {
        if (!editor1) {
          editor1 = req.getCellEditor(grid.columns[0]);
          req.failure();
        } else {
          editor2 = req.getCellEditor(grid.columns[0]);
          req.success();
        }
      };
      grid.editor.editRow(0);

      //Save fails
      grid.editor.save();
      //Save succeeds
      grid.editor.save();

      assert.equal(editor1, editor2);
    });

    // fails because of replacing columns doesn't render row contents,
    // see https://trello.com/c/8e27Tv3q/58-v-grid-bugs
    it.skip('editor: default editor', function() {
      grid.editor.enabled = true;
      grid.editor.editRow(0);

      return grid.then(function() {
        assert.equal("Grid", grid.querySelector('tbody tr td').textContent);
        assert.equal("Grid", grid.querySelector('input').value);

        grid.querySelector('input').value = "Foo";
        grid.editor.save();
      });

    });

    it('editor: getCellEditor and read-only columns', function() {
      grid.columns[0].readOnly = true;

      sinon.spy(editorHandler, "getCellEditor");

      grid.editor.handler = editorHandler;
      grid.editor.enabled = true;
      grid.editor.editRow(0);

      assert.isTrue(editorHandler.getCellEditor.calledOnce);

      editorHandler.getCellEditor.restore();
    });
  });
</script>

</body>
</html>