// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

module.exports = {

  'Displays an error if reduce is not possible': function (client) {
    /*jshint multistr: true */
    var waitTime = 10000,
        newDatabaseName = client.globals.testDatabaseName,
        baseUrl = client.globals.test_settings.launch_url,
        dropDownElement = '#header-dropdown-menu';

    client
      .loginToGUI()
      .populateDatabase(newDatabaseName)
      .url(baseUrl + '/#/database/' + newDatabaseName + '/_all_docs')
      .waitForElementPresent(dropDownElement, waitTime, false)
      .click(dropDownElement + ' a')
      .click(dropDownElement + ' a[href*="new_view"]')
      .waitForElementPresent('.editor-wrapper', waitTime, false)
      .setValue('#new-ddoc', 'test_design_doc-selenium-bad-reduce')
      .clearValue('#index-name')
      .setValue('#index-name', 'hasenindex')
      .execute('\
        var editor = ace.edit("map-function");\
        editor.getSession().setValue("function (doc) { emit(\'boom\', doc._id); }");\
      ')
      .click('#reduce-function-selector')
      .keys(['\uE013', '\uE013', '\uE013', '\uE013', '\uE006'])
      .execute('$(".save")[0].scrollIntoView();')
      .click('button.btn-success.save')
      .waitForElementVisible('.alert-error', waitTime, false)
      .assert.containsText('.alert-error', 'builtin _sum function requires map values to be numbers or lists of numbers')
      .end();
  },

  'Visit url of broken view displays error': function (client) {
    var waitTime = 10000,
        newDatabaseName = client.globals.testDatabaseName,
        baseUrl = client.globals.test_settings.launch_url;

    client
      .loginToGUI()
      .populateDatabase(newDatabaseName)
      .url(baseUrl + '/#/database/' + newDatabaseName + '/_design/brokenview/_view/brokenview')
      .waitForElementVisible('.alert-error', waitTime, false)
      .assert.containsText('.alert-error', 'builtin _sum function requires map values to be numbers or lists of numbers')
      .end();
  }


};

@@ -997,170 +997,16 @@
 + '/
-_all_docs')%0A      .waitForElementPresent(dropDownElement, waitTime, false)%0A      .click(dropDownElement + ' a')%0A      .click(dropDownElement + ' a%5Bhref*=%22
 new_view
 %22%5D')
@@ -1005,78 +1005,11 @@
 view
-%22%5D
 ')%0A
-      .waitForElementPresent('.editor-wrapper', waitTime, false)%0A
     
@@ -1609,32 +1609,24 @@
 rt-error', '
-builtin 
 _sum functio
@@ -1628,32 +1628,37 @@
 nction requires 
+that 
 map values to be
@@ -1656,33 +1656,10 @@
 ues 
-to be numbers or lists of
+be
  num
@@ -2173,16 +2173,8 @@
 ', '
-builtin 
 _sum
@@ -2192,16 +2192,21 @@
 equires 
+that 
 map valu
@@ -2212,33 +2212,10 @@
 ues 
-to be numbers or lists of
+be
  num

fix test for 2.0