123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- import assert from 'assert';
- import moment from 'moment'; // jshint ignore:line
- import React from 'react'; // jshint ignore:line
- import FormLoading from 'misago/components/options/change-username/form-loading'; // jshint ignore:line
- import FormLocked from 'misago/components/options/change-username/form-locked'; // jshint ignore:line
- import Form from 'misago/components/options/change-username/form'; // jshint ignore:line
- import Root from 'misago/components/options/change-username/root'; // jshint ignore:line
- import misago from 'misago/index';
- import snackbar from 'misago/services/snackbar';
- import store from 'misago/services/store';
- import * as testUtils from 'misago/utils/test-utils';
- let snackbarStore = null;
- let user = testUtils.mockUser();
- user.acl.name_changes_expire = 2;
- describe("Change Username Form", function() {
- beforeEach(function() {
- snackbarStore = testUtils.snackbarStoreMock();
- snackbar.init(snackbarStore);
- testUtils.initEmptyStore(store);
- });
- afterEach(function() {
- testUtils.unmountComponents();
- testUtils.snackbarClear(snackbar);
- $.mockjax.clear();
- });
- it("renders", function(done) {
- /* jshint ignore:start */
- let options = {
- changes_left: 5,
- length_min: 3,
- length_max: 14,
- next_on: null
- };
- testUtils.render(
- <Form user={user}
- options={options} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .form-horizontal', function() {
- assert.ok(true, "component renders");
- assert.equal($('#test-mount form .help-block').text().trim(),
- "You can change your username 5 more times. Used changes redeem after 2 days.",
- "valid help text is displayed in form");
- done();
- });
- });
- it("handles empty submit", function(done) {
- /* jshint ignore:start */
- let options = {
- changes_left: 5,
- length_min: 3,
- length_max: 14,
- next_on: null
- };
- testUtils.render(
- <Form user={user}
- options={options} />
- );
- /* jshint ignore:end */
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: "This field is required.",
- type: 'error'
- }, "error message was shown");
- done();
- });
- testUtils.onElement('#test-mount form', function() {
- testUtils.simulateSubmit('#test-mount form');
- });
- });
- it("handles invalid submit", function(done) {
- /* jshint ignore:start */
- let options = {
- changes_left: 5,
- length_min: 10,
- length_max: 14,
- next_on: null
- };
- testUtils.render(
- <Form user={user}
- options={options} />
- );
- /* jshint ignore:end */
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: "Username must be at least 10 characters long.",
- type: 'error'
- }, "error message was shown");
- done();
- });
- testUtils.onElement('#test-mount form', function() {
- testUtils.simulateChange('#test-mount form #id_username', 'NewName');
- testUtils.simulateSubmit('#test-mount form');
- });
- });
- it("handles backend rejection", function(done) {
- $.mockjax({
- url: user.api_url.username,
- status: 400,
- responseText: {
- detail: "Lol nope!"
- }
- });
- /* jshint ignore:start */
- let options = {
- changes_left: 5,
- length_min: 3,
- length_max: 14,
- next_on: null
- };
- testUtils.render(
- <Form user={user}
- options={options} />
- );
- /* jshint ignore:end */
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: "Lol nope!",
- type: 'error'
- }, "error message from backend was shown");
- done();
- });
- testUtils.onElement('#test-mount form', function() {
- testUtils.simulateChange('#test-mount form #id_username', 'Newt');
- testUtils.simulateSubmit('#test-mount form');
- });
- });
- it("handles backend error", function(done) {
- $.mockjax({
- url: user.api_url.username,
- status: 500
- });
- /* jshint ignore:start */
- let options = {
- changes_left: 5,
- length_min: 3,
- length_max: 14,
- next_on: null
- };
- testUtils.render(
- <Form user={user}
- options={options} />
- );
- /* jshint ignore:end */
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: "Unknown error has occured.",
- type: 'error'
- }, "error message from backend was shown");
- done();
- });
- testUtils.onElement('#test-mount form', function() {
- testUtils.simulateChange('#test-mount form #id_username', 'Newt');
- testUtils.simulateSubmit('#test-mount form');
- });
- });
- it("handles successfull submission", function(done) { // jshint ignore:line
- $.mockjax({
- url: user.api_url.username,
- status: 200,
- responseText: {
- username: 'Newt',
- slug: 'newt',
- options: {
- changes_left: 4,
- length_min: 3,
- length_max: 14,
- next_on: null
- }
- }
- });
- /* jshint ignore:start */
- let options = {
- changes_left: 5,
- length_min: 3,
- length_max: 14,
- next_on: null
- };
- let callback = function(username, slug, options) {
- assert.equal(username, 'Newt', "new username is passed to callback");
- assert.equal(slug, 'newt', "new slug is passed to callback");
- assert.deepEqual(options, {
- changes_left: 4,
- length_min: 3,
- length_max: 14,
- next_on: null
- }, "new options are passed to callback");
- done();
- };
- testUtils.render(
- <Form user={user}
- options={options}
- complete={callback} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount form', function() {
- testUtils.simulateChange('#test-mount form #id_username', 'newt');
- testUtils.simulateSubmit('#test-mount form');
- });
- });
- });
- describe("Change Username Form Locked", function() {
- afterEach(function() {
- testUtils.unmountComponents();
- });
- it("renders", function(done) {
- /* jshint ignore:start */
- let options = {
- changes_left: 5,
- length_min: 3,
- length_max: 14,
- next_on: null
- };
- testUtils.render(<FormLocked options={options} />);
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .panel-message-body', function() {
- assert.ok(true, "component renders");
- assert.equal($('#test-mount .help-block').text().trim(),
- "You have used up available name changes.",
- "valid help text is displayed in message");
- done();
- });
- });
- it("renders with next change message", function(done) {
- /* jshint ignore:start */
- let options = {
- changes_left: 5,
- length_min: 3,
- length_max: 14,
- next_on: moment().add(5, 'days')
- };
- testUtils.render(<FormLocked options={options} />);
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .panel-message-body', function() {
- assert.ok(true, "component renders");
- assert.equal($('#test-mount .help-block').text().trim(),
- "You will be able to change your username in 5 days.",
- "valid help text is displayed in message");
- done();
- });
- });
- });
- describe("Change Username Form Loading", function() {
- afterEach(function() {
- testUtils.unmountComponents();
- });
- it("renders", function(done) {
- /* jshint ignore:start */
- testUtils.render(<FormLoading />);
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .panel-body-loading', function() {
- assert.ok(true, "component renders");
- done();
- });
- });
- });
- describe("Change Username Integration", function() {
- beforeEach(function() {
- snackbarStore = testUtils.snackbarStoreMock();
- snackbar.init(snackbarStore);
- testUtils.initEmptyStore(store);
- misago._context = {
- USERNAME_CHANGES_API: '/test-api/name-history/'
- };
- });
- afterEach(function() {
- testUtils.unmountComponents();
- testUtils.snackbarClear(snackbar);
- $.mockjax.clear();
- });
- it("renders", function(done) {
- $.mockjax({
- url: user.api_url.username,
- status: 200,
- responseText: {
- changes_left: 2,
- length_min: 3,
- length_max: 14,
- next_on: null
- }
- });
- $.mockjax({
- url: '/test-api/name-history/?user=' + user.id,
- status: 200,
- responseText: {
- results: []
- }
- });
- /* jshint ignore:start */
- testUtils.render(
- <Root user={user}
- username-history={[]} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .username-history.ui-ready', function() {
- assert.ok(true, "root component renders");
- done();
- });
- });
- it("renders with no changes left", function(done) {
- $.mockjax({
- url: user.api_url.username,
- status: 200,
- responseText: {
- changes_left: 0,
- length_min: 3,
- length_max: 14,
- next_on: null
- }
- });
- $.mockjax({
- url: '/test-api/name-history/?user=' + user.id,
- status: 200,
- responseText: {
- results: []
- }
- });
- /* jshint ignore:start */
- testUtils.render(
- <Root user={user}
- username-history={[]} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .panel-message-body', function() {
- assert.ok(true, "root component renders");
- assert.equal($('#test-mount .help-block').text().trim(),
- "You have used up available name changes.",
- "valid help text is displayed in message");
- done();
- });
- });
- it("handles username change", function(done) {
- $.mockjax({
- url: user.api_url.username,
- status: 200,
- type: 'GET',
- responseText: {
- changes_left: 2,
- length_min: 3,
- length_max: 14,
- next_on: null
- }
- });
- $.mockjax({
- url: user.api_url.username,
- status: 200,
- type: 'POST',
- responseText: {
- username: 'Newt',
- slug: 'newt',
- options: {
- changes_left: 2,
- length_min: 3,
- length_max: 14,
- next_on: null
- }
- }
- });
- $.mockjax({
- url: '/test-api/name-history/?user=' + user.id,
- status: 200,
- responseText: {
- results: []
- }
- });
- /* jshint ignore:start */
- testUtils.render(
- <Root user={user}
- username-history={[]} />
- );
- /* jshint ignore:end */
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: "Your username has been changed successfully.",
- type: 'success'
- }, "error message was shown");
- done();
- });
- testUtils.onElement('#test-mount form #id_username', function() {
- testUtils.simulateChange('#test-mount form #id_username', 'newt');
- testUtils.simulateSubmit('#test-mount form');
- });
- });
- });
|