Files
zulip/frontend_tests/casper_tests/12-toggle-message-editing.js
Steve Howell 8530ed0b5e org settings: Make button-clicking code more clear.
On the "Organization settings" page, we had two Save buttons
for admins that had identical markup.  This was confusing for
people reading the code.  Now the two buttons have different
markup and individual, targeted click handlers (albeit still
calling the same function to do most of the work).

The context of this fix is that I was debugging a
Casper flake where our Casper tests were essentially clicking
on the same button twice.  Depending on the timing,
the second button click could cause a "No changes saved!"
behavior that confused the Casper test.  It is unclear whether
Casper was clicking both buttons here (in which case this fix
is necessary) or the same button twice (in which case this fix
just removes a nasty red herring for debugging).

The code still has the flaw that both buttons basically submit
the same data to the server, despite the appearance on the page
that there are two forms.  The best fix for that is probably
just to move the Language/Notifications stuff to another
panel.  I wanted to avoid touching this code altogether, but
the minor modifications here were necessary to improve the
Capser testing situation.
2018-01-16 13:25:19 -05:00

292 lines
10 KiB
JavaScript

var common = require('../casper_lib/common.js').common;
function heading(heading_str) {
casper.then(function () {
casper.test.info(heading_str);
});
}
function submit() {
// Casper 1.1.4 has a strange bug related to dispatching functions
// twice. We call save_organization_settings() to try to minimize
// the moving parts involved in troubleshooting.
casper.evaluate(function () {
settings_org.save_organization_settings();
});
}
function submit_checked() {
casper.then(function () {
casper.waitUntilVisible('input:checked[type="checkbox"][id="id_realm_allow_message_editing"] + span', function () {
submit();
});
});
}
function submit_unchecked() {
casper.then(function () {
casper.waitUntilVisible('input:not(:checked)[type="checkbox"][id="id_realm_allow_message_editing"] + span', function () {
submit();
});
});
}
common.start_and_log_in();
// For clarity these should be different than what 08-edit uses, until
// we find a more robust way to manage DB state between tests.
var content1 = 'admin: edit test message 1';
var content2 = 'admin: edit test message 2';
// send two messages
common.then_send_message('stream', {
stream: 'Verona',
subject: 'edits',
content: content1,
});
common.then_send_message('stream', {
stream: 'Verona',
subject: 'edits',
content: content2,
});
casper.then(function () {
casper.waitForSelectorText('#zhome .message_row', content1);
casper.waitForSelectorText('#zhome .message_row', content2);
});
// wait for message to be sent
casper.then(function () {
casper.waitFor(function () {
return casper.evaluate(function () {
return !current_msg_list.last().locally_echoed;
});
});
});
// edit the last message just sent
casper.then(function () {
casper.evaluate(function () {
var msg = $('#zhome .message_row:last');
msg.find('.info').click();
$('.popover_edit_message').click();
});
});
var edited_value = 'admin tests: test edit';
casper.then(function () {
casper.waitUntilVisible(".message_edit_content", function () {
casper.evaluate(function (edited_value) {
var msg = $('#zhome .message_row:last');
msg.find('.message_edit_content').val(edited_value);
msg.find('.message_edit_save').click();
}, edited_value);
});
});
casper.then(function () {
// check that the message was indeed edited
casper.waitWhileVisible("textarea.message_edit_content", function () {
casper.test.assertSelectorHasText(".last_message .message_content", edited_value);
});
});
// go to admin page
common.then_click('#settings-dropdown');
common.then_click('a[href^="#organization"]');
casper.then(function () {
casper.waitForSelector('#settings_overlay_container.show', function () {
casper.test.info('Organization page is active');
casper.test.assertUrlMatch(/^http:\/\/[^/]+\/#organization/, 'URL suggests we are on organization page');
});
});
// DEACTIVATE
heading("DEACTIVATE");
common.then_click("li[data-section='organization-settings']");
// deactivate "allow message editing"
common.then_click('input[type="checkbox"][id="id_realm_allow_message_editing"] + span');
submit_unchecked();
casper.then(function () {
casper.waitUntilVisible('#admin-realm-message-editing-status', function () {
casper.test.assertSelectorHasText('#admin-realm-message-editing-status', 'Users can no longer edit their past messages!');
casper.test.assertEval(function () {
return !(document.querySelector('input[type="checkbox"][id="id_realm_allow_message_editing"]').checked);
}, 'Allow message editing Setting de-activated');
});
});
// go back to home page
casper.then(function () {
casper.click('.settings-header .exit');
});
// VIEW SOURCE
heading("VIEW SOURCE");
// Check that edit link has changed to "View source" in the popover menu
// TODO: also check that the edit icon no longer appears next to the message
casper.then(function () {
// This somehow makes the "View source" test deterministic. It seems that
// we are waiting on a wrong condition somewhere.
casper.wait(1000);
});
casper.then(function () {
casper.waitUntilVisible('.message_row');
// Note that this could have a false positive, e.g. if all the messages aren't
// loaded yet. See Issue #1243
casper.evaluate(function () {
var msg = $('#zhome .message_row:last');
msg.find('.info').click();
});
casper.waitUntilVisible('.popover_edit_message', function () {
casper.test.assertSelectorHasText('.popover_edit_message', 'View source');
});
casper.evaluate(function () {
var msg = $('#zhome .message_row:last');
msg.find('.info').click();
});
});
// REACTIVATE
heading("REACTIVATE");
// go back to admin page, and reactivate "allow message editing"
common.then_click('#settings-dropdown');
common.then_click('a[href^="#organization"]');
common.then_click("li[data-section='organization-settings']");
common.then_click('input[type="checkbox"][id="id_realm_allow_message_editing"] + span');
submit_checked();
casper.then(function () {
casper.waitUntilVisible('#admin-realm-message-editing-status', function () {
casper.test.assertSelectorHasText('#admin-realm-message-editing-status', 'Users can now edit topics for all their messages, and the content of messages which are less than 10 minutes old.');
casper.test.assertEval(function () {
return document.querySelector('input[type="checkbox"][id="id_realm_allow_message_editing"]').checked;
}, 'Allow message editing Setting re-activated');
});
});
// DEACTIVATE
heading("DEACTIVATE");
// go to admin page
casper.then(function () {
casper.test.info('Organization page');
casper.click('a[href^="#organization"]');
casper.test.assertUrlMatch(/^http:\/\/[^\/]+\/#organization/, 'URL suggests we are on organization page');
casper.test.assertExists('#settings_overlay_container.show', 'Organization page is active');
});
casper.then(function () {
casper.waitUntilVisible('form.admin-realm-form button.button');
});
// deactivate message editing
casper.then(function () {
casper.waitUntilVisible('input[type="checkbox"][id="id_realm_allow_message_editing"] + span', function () {
casper.evaluate(function () {
$('input[type="text"][id="id_realm_message_content_edit_limit_minutes"]').val('4');
});
});
});
common.then_click('input[type="checkbox"][id="id_realm_allow_message_editing"] + span');
submit_unchecked();
casper.then(function () {
casper.waitUntilVisible('#admin-realm-message-editing-status', function () {
casper.test.assertSelectorHasText('#admin-realm-message-editing-status', 'Users can no longer edit their past messages!');
casper.test.assertEval(function () {
return !(document.querySelector('input[type="checkbox"][id="id_realm_allow_message_editing"]').checked);
}, 'Allow message editing Setting de-activated');
casper.test.assertEval(function () {
return $('input[type="text"][id="id_realm_message_content_edit_limit_minutes"]').val() === '4';
}, 'Message content edit limit now 4');
});
});
// REACTIVATE
heading("REACTIVATE");
common.then_click('input[type="checkbox"][id="id_realm_allow_message_editing"] + span');
submit_checked();
casper.then(function () {
casper.waitUntilVisible('#admin-realm-message-editing-status', function () {
casper.test.assertSelectorHasText('#admin-realm-message-editing-status', 'Users can now edit topics for all their messages, and the content of messages which are less than 4 minutes old.');
casper.test.assertEval(function () {
return document.querySelector('input[type="checkbox"][id="id_realm_allow_message_editing"]').checked;
}, 'Allow message editing Setting activated');
casper.test.assertEval(function () {
return $('input[type="text"][id="id_realm_message_content_edit_limit_minutes"]').val() === '4';
}, 'Message content edit limit still 4');
});
});
// SET LIMIT TO 0
heading("NO LIMIT");
casper.then(function () {
// allow arbitrary message editing
casper.waitUntilVisible('input[type="checkbox"][id="id_realm_allow_message_editing"] + span', function () {
casper.evaluate(function () {
$('input[type="text"][id="id_realm_message_content_edit_limit_minutes"]').val('0');
});
});
});
submit_checked();
casper.then(function () {
casper.waitUntilVisible('#admin-realm-message-editing-status', function () {
casper.test.assertSelectorHasText('#admin-realm-message-editing-status', 'Users can now edit the content and topics of all their past messages!');
casper.test.assertEval(function () {
return document.querySelector('input[type="checkbox"][id="id_realm_allow_message_editing"]').checked;
}, 'Allow message editing Setting still activated');
casper.test.assertEval(function () {
return $('input[type="text"][id="id_realm_message_content_edit_limit_minutes"]').val() === '0';
}, 'Message content edit limit is 0');
});
});
// ILLEGAL LIMIT
heading("ILLEGAL LIMIT");
casper.then(function () {
// disallow message editing, with illegal edit limit value. should be fixed by admin.js
casper.waitUntilVisible('input[type="checkbox"][id="id_realm_allow_message_editing"] + span', function () {
casper.evaluate(function () {
$('input[type="text"][id="id_realm_message_content_edit_limit_minutes"]').val('moo');
});
});
});
common.then_click('input[type="checkbox"][id="id_realm_allow_message_editing"] + span');
submit_unchecked();
casper.then(function () {
casper.waitUntilVisible('#admin-realm-message-editing-status', function () {
casper.test.assertSelectorHasText('#admin-realm-message-editing-status', 'Users can no longer edit their past messages!');
casper.test.assertEval(function () {
return !(document.querySelector('input[type="checkbox"][id="id_realm_allow_message_editing"]').checked);
}, 'Allow message editing Setting de-activated');
casper.test.assertEval(function () {
return $('input[type="text"][id="id_realm_message_content_edit_limit_minutes"]').val() === '10';
}, 'Message content edit limit has been reset to its default');
});
});
common.then_log_out();
casper.run(function () {
casper.test.done();
});