123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- import assert from 'assert';
- import moment from 'moment';
- import React from 'react'; // jshint ignore:line
- import UsernameHistory from 'misago/components/profile/username-history'; // jshint ignore:line
- import misago from 'misago/index';
- import reducer from 'misago/reducers/username-history';
- import snackbar from 'misago/services/snackbar';
- import store from 'misago/services/store';
- import * as testUtils from 'misago/utils/test-utils';
- let snackbarStore = null;
- /* jshint ignore:start */
- let userMock = {
- id: 42,
- username: 'TestUser',
- avatar_hash: 'abcdfefa'
- };
- let profileMock = {
- id: 123,
- username: 'BobBoberson',
- avatar_hash: 'abcdfefa'
- };
- /* jshint ignore:end */
- describe("User Profile Username History", function() {
- beforeEach(function() {
- misago._context = {
- USERNAME_CHANGES_API: '/test-api/username-history/'
- };
- snackbarStore = testUtils.snackbarStoreMock();
- snackbar.init(snackbarStore);
- store.constructor();
- store.addReducer('username-history', reducer, []);
- store.init();
- });
- afterEach(function() {
- testUtils.unmountComponents();
- $.mockjax.clear();
- });
- it("preloads empty", function(done) {
- misago._context.PROFILE_NAME_HISTORY = {
- count: 0,
- more: 0,
- page: 1,
- pages: 1,
- results: []
- };
- /* jshint ignore:start */
- testUtils.render(
- <UsernameHistory user={userMock}
- profile={profileMock}
- username-history={[]} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .ui-ready', function(element) {
- assert.ok(element.length, "element renders");
- assert.equal($('#test-mount h3').text(),
- "BobBoberson's username was changed 0 times.",
- "component has valid header");
- assert.equal(element.find('.empty-message').text(),
- "BobBoberson's username was never changed.",
- "empty message was displayed");
- done();
- });
- });
- it("preloads empty (owned)", function(done) {
- misago._context.PROFILE_NAME_HISTORY = {
- count: 0,
- more: 0,
- page: 1,
- pages: 1,
- results: []
- };
- /* jshint ignore:start */
- testUtils.render(
- <UsernameHistory user={Object.assign({}, userMock, {id: 123})}
- profile={profileMock}
- username-history={[]} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .ui-ready', function(element) {
- assert.ok(element.length, "element renders");
- assert.equal($('#test-mount h3').text(),
- "Your username was changed 0 times.",
- "component has valid header");
- assert.equal(element.find('.empty-message').text(),
- "No name changes have been recorded for your account.",
- "empty message was displayed");
- done();
- });
- });
- it("loads empty", function(done) {
- $.mockjax({
- url: '/test-api/username-history/?user=123&search=&page=1',
- status: 200,
- responseText: {
- count: 0,
- more: 0,
- page: 1,
- pages: 1,
- results: []
- }
- });
- /* jshint ignore:start */
- testUtils.render(
- <UsernameHistory user={userMock}
- profile={profileMock}
- username-history={[]} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .ui-ready', function(element) {
- assert.ok(element.length, "element renders");
- assert.equal(element.find('.empty-message').text(),
- "BobBoberson's username was never changed.",
- "empty message was displayed");
- done();
- });
- });
- it("loads empty (owned)", function(done) {
- $.mockjax({
- url: '/test-api/username-history/?user=123&search=&page=1',
- status: 200,
- responseText: {
- count: 0,
- more: 0,
- page: 1,
- pages: 1,
- results: []
- }
- });
- /* jshint ignore:start */
- testUtils.render(
- <UsernameHistory user={Object.assign({}, userMock, {id: 123})}
- profile={profileMock}
- username-history={[]} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .ui-ready', function(element) {
- assert.ok(element.length, "element renders");
- assert.equal(element.find('.empty-message').text(),
- "No name changes have been recorded for your account.",
- "empty message was displayed");
- done();
- });
- });
- it("loads username changes", function(done) {
- $.mockjax({
- url: '/test-api/username-history/?user=123&search=&page=1',
- status: 200,
- responseText: {
- count: 5,
- more: 0,
- page: 1,
- pages: 1,
- results: [1, 2, 3, 4, 5].map(function(id) {
- return {
- id: id,
- changed_by: {
- id: 1,
- username: "rafalp",
- slug: "rafalp",
- avatar_hash: "5c6a04b4",
- absolute_url: "/user/rafalp-1/"
- },
- changed_by_username: "rafalp",
- changed_on: moment().format(),
- new_username: "Newt",
- old_username: "LoremIpsum"
- };
- })
- }
- });
- /* jshint ignore:start */
- testUtils.render(
- <UsernameHistory user={userMock}
- profile={profileMock}
- username-history={[]} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .username-history.ui-ready', function() {
- assert.equal($('#test-mount h3').text(),
- "BobBoberson's username was changed 5 times.",
- "component has valid header");
- assert.equal(store.getState()['username-history'].length, 5,
- "component renders with five items");
- done();
- });
- });
- it("loads more changes", function(done) {
- $.mockjax({
- url: '/test-api/username-history/?user=123&search=&page=1',
- status: 200,
- responseText: {
- count: 10,
- more: 5,
- page: 1,
- pages: 2,
- results: [1, 2, 3, 4, 5].map(function(id) {
- return {
- id: id,
- changed_by: {
- id: 1,
- username: "rafalp",
- slug: "rafalp",
- avatar_hash: "5c6a04b4",
- absolute_url: "/user/rafalp-1/"
- },
- changed_by_username: "rafalp",
- changed_on: moment().format(),
- new_username: "Newt",
- old_username: "LoremIpsum"
- };
- })
- }
- });
- $.mockjax({
- url: '/test-api/username-history/?user=123&search=&page=2',
- status: 200,
- responseText: {
- count: 10,
- more: 0,
- page: 2,
- pages: 2,
- results: [1, 2, 3, 4, 5].map(function(id) {
- return {
- id: 5 + id,
- changed_by: {
- id: 1,
- username: "rafalp",
- slug: "rafalp",
- avatar_hash: "5c6a04b4",
- absolute_url: "/user/rafalp-1/"
- },
- changed_by_username: "rafalp",
- changed_on: moment().format(),
- new_username: "Newt",
- old_username: "LoremIpsum"
- };
- })
- }
- });
- /* jshint ignore:start */
- testUtils.render(
- <UsernameHistory user={userMock}
- profile={profileMock}
- username-history={[]} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .pager-more .btn', function() {
- assert.equal($('#test-mount h3').text(),
- "BobBoberson's username was changed 10 times.",
- "component has valid header");
- testUtils.simulateClick('#test-mount .pager-more .btn');
- window.setTimeout(function() {
- assert.equal(store.getState()['username-history'].length, 10,
- "component renders with ten items");
- done();
- }, 300);
- });
- });
- it("loads search results", function(done) {
- $.mockjax({
- url: '/test-api/username-history/?user=123&search=&page=1',
- status: 200,
- responseText: {
- count: 10,
- more: 5,
- page: 1,
- pages: 2,
- results: [1, 2, 3, 4, 5].map(function(id) {
- return {
- id: id,
- changed_by: {
- id: 1,
- username: "rafalp",
- slug: "rafalp",
- avatar_hash: "5c6a04b4",
- absolute_url: "/user/rafalp-1/"
- },
- changed_by_username: "rafalp",
- changed_on: moment().format(),
- new_username: "Newt",
- old_username: "LoremIpsum"
- };
- })
- }
- });
- $.mockjax({
- url: '/test-api/username-history/?user=123&search=test&page=1',
- status: 200,
- responseText: {
- count: 3,
- more: 0,
- page: 1,
- pages: 1,
- results: [1, 2, 3].map(function(id) {
- return {
- id: 10 + id,
- changed_by: {
- id: 1,
- username: "rafalp",
- slug: "rafalp",
- avatar_hash: "5c6a04b4",
- absolute_url: "/user/rafalp-1/"
- },
- changed_by_username: "rafalp",
- changed_on: moment().format(),
- new_username: "Newt",
- old_username: "LoremIpsum"
- };
- })
- }
- });
- /* jshint ignore:start */
- testUtils.render(
- <UsernameHistory user={userMock}
- profile={profileMock}
- username-history={[]} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .pager-more .btn', function() {
- assert.equal($('#test-mount h3').text(),
- "BobBoberson's username was changed 10 times.",
- "component has valid header");
- testUtils.simulateChange('#test-mount .form-control', 'test');
- window.setTimeout(function() {
- assert.equal(store.getState()['username-history'].length, 3,
- "component renders with three found items");
- assert.equal($('#test-mount h3').text(), "Found 3 username changes.",
- "component has valid header");
- done();
- }, 300);
- });
- });
- it("loads empty search results", function(done) {
- $.mockjax({
- url: '/test-api/username-history/?user=123&search=&page=1',
- status: 200,
- responseText: {
- count: 10,
- more: 5,
- page: 1,
- pages: 2,
- results: [1, 2, 3, 4, 5].map(function(id) {
- return {
- id: id,
- changed_by: {
- id: 1,
- username: "rafalp",
- slug: "rafalp",
- avatar_hash: "5c6a04b4",
- absolute_url: "/user/rafalp-1/"
- },
- changed_by_username: "rafalp",
- changed_on: moment().format(),
- new_username: "Newt",
- old_username: "LoremIpsum"
- };
- })
- }
- });
- $.mockjax({
- url: '/test-api/username-history/?user=123&search=test&page=1',
- status: 200,
- responseText: {
- count: 0,
- more: 0,
- page: 1,
- pages: 1,
- results: []
- }
- });
- /* jshint ignore:start */
- testUtils.render(
- <UsernameHistory user={userMock}
- profile={profileMock}
- username-history={[]} />
- );
- /* jshint ignore:end */
- testUtils.onElement('#test-mount .pager-more .btn', function() {
- assert.equal($('#test-mount h3').text(),
- "BobBoberson's username was changed 10 times.",
- "component has valid header");
- testUtils.simulateChange('#test-mount .form-control', 'test');
- window.setTimeout(function() {
- assert.equal(store.getState()['username-history'].length, 0,
- "store is emptied");
- assert.equal($('#test-mount h3').text(), "Found 0 username changes.",
- "component has valid header");
- done();
- }, 300);
- });
- });
- it("handles backend error", function(done) {
- $.mockjax({
- url: '/test-api/username-history/?user=123&search=&page=1',
- status: 500
- });
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: "Unknown error has occured.",
- type: 'error'
- }, "error message was shown");
- done();
- });
- /* jshint ignore:start */
- testUtils.render(
- <UsernameHistory user={userMock}
- profile={profileMock}
- username-history={[]} />
- );
- /* jshint ignore:end */
- });
- it("handles backend rejection", function(done) {
- $.mockjax({
- url: '/test-api/username-history/?user=123&search=&page=1',
- status: 403,
- responseText: {
- detail: "You can't see it yo!"
- }
- });
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: "You can't see it yo!",
- type: 'error'
- }, "error message was shown");
- done();
- });
- /* jshint ignore:start */
- testUtils.render(
- <UsernameHistory user={userMock}
- profile={profileMock}
- username-history={[]} />
- );
- /* jshint ignore:end */
- });
- });
|