gallery.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import ChangeAvatarGallery, { Gallery, GalleryItem } from 'misago/components/change-avatar/gallery'; // jshint ignore:line
  4. import misago from 'misago/index';
  5. import snackbar from 'misago/services/snackbar';
  6. import * as testUtils from 'misago/utils/test-utils';
  7. let snackbarStore = null;
  8. let apiResponse = {
  9. "crop_tmp": false,
  10. "galleries": [
  11. {
  12. "images": [
  13. "avatars/Nature/arctic_fox.jpg",
  14. "avatars/Nature/baby_fox.jpg",
  15. "avatars/Nature/blackbird.jpg",
  16. "avatars/Nature/rabbit.jpg",
  17. "avatars/Nature/serval.jpg"
  18. ],
  19. "name": "Nature"
  20. },
  21. {
  22. "images": [
  23. "avatars/Space/andromeda.jpg",
  24. "avatars/Space/antennae_galaxies.jpg",
  25. "avatars/Space/barred_spiral_galaxy.jpg",
  26. "avatars/Space/messier_74.jpg",
  27. "avatars/Space/ngc_1672.jpg",
  28. "avatars/Space/ngc_4414.jpg"
  29. ],
  30. "name": "Space"
  31. }
  32. ],
  33. "crop_org": false,
  34. "upload": {
  35. "allowed_extensions": [
  36. ".gif",
  37. ".png",
  38. ".jpg",
  39. ".jpeg"
  40. ],
  41. "limit": 750000,
  42. "allowed_mime_types": [
  43. "image/gif",
  44. "image/jpeg",
  45. "image/png"
  46. ]
  47. },
  48. "generated": true,
  49. "gravatar": true
  50. };
  51. describe("Change Avatar Gallery", function() {
  52. beforeEach(function() {
  53. snackbarStore = testUtils.snackbarStoreMock();
  54. snackbar.init(snackbarStore);
  55. misago._context = {
  56. 'user': {
  57. 'id': 123,
  58. 'avatar_hash': 'aabbccdd',
  59. 'avatar_api_url': '/test-api/users/123/avatar/'
  60. }
  61. };
  62. });
  63. afterEach(function() {
  64. testUtils.unmountComponents();
  65. testUtils.snackbarClear(snackbar);
  66. $.mockjax.clear();
  67. });
  68. it("renders", function(done) {
  69. /* jshint ignore:start */
  70. testUtils.render(
  71. <ChangeAvatarGallery user={misago.get('user')}
  72. options={apiResponse} />
  73. );
  74. /* jshint ignore:end */
  75. testUtils.onElement('#test-mount .modal-avatar-gallery', function() {
  76. assert.ok(true, "component renders");
  77. done();
  78. });
  79. });
  80. it("handles backend rejection", function(done) {
  81. $.mockjax({
  82. url: '/test-api/users/123/avatar/',
  83. status: 400,
  84. responseText: {
  85. detail: "Lol nope!"
  86. }
  87. });
  88. snackbarStore.callback(function(message) {
  89. assert.deepEqual(message, {
  90. message: "Lol nope!",
  91. type: 'error'
  92. }, "error message was shown");
  93. done();
  94. });
  95. let component = null;
  96. /* jshint ignore:start */
  97. component = testUtils.render(
  98. <ChangeAvatarGallery user={misago.get('user')}
  99. options={apiResponse} />
  100. );
  101. /* jshint ignore:end */
  102. testUtils.onElement('#test-mount .btn-avatar', function() {
  103. testUtils.simulateClick('#test-mount .btn-avatar');
  104. });
  105. testUtils.onElement('#test-mount .avatar-selected', function() {
  106. assert.equal(component.state.selection, "avatars/Nature/arctic_fox.jpg",
  107. "avatar selection callback works");
  108. testUtils.simulateClick('#test-mount .btn-primary');
  109. });
  110. });
  111. it("handles backend error", function(done) { // jshint ignore:line
  112. $.mockjax({
  113. url: '/test-api/users/123/avatar/',
  114. status: 403,
  115. responseText: {
  116. detail: "Avatar can't be changed at this time!"
  117. }
  118. });
  119. snackbarStore.callback(function(message) {
  120. assert.deepEqual(message, {
  121. message: "Lol nope!",
  122. type: 'error'
  123. }, "error message was shown");
  124. done();
  125. });
  126. let component = null;
  127. /* jshint ignore:start */
  128. let showError = function(error) {
  129. assert.equal(error.detail, "Avatar can't be changed at this time!",
  130. "callback was called with backend error message");
  131. done()
  132. };
  133. component = testUtils.render(
  134. <ChangeAvatarGallery user={misago.get('user')}
  135. options={apiResponse}
  136. showError={showError} />
  137. );
  138. /* jshint ignore:end */
  139. testUtils.onElement('#test-mount .btn-avatar', function() {
  140. testUtils.simulateClick('#test-mount .btn-avatar');
  141. });
  142. testUtils.onElement('#test-mount .avatar-selected', function() {
  143. assert.equal(component.state.selection, "avatars/Nature/arctic_fox.jpg",
  144. "avatar selection callback works");
  145. testUtils.simulateClick('#test-mount .btn-primary');
  146. });
  147. });
  148. it("selects and submits avatar", function(done) { // jshint ignore:line
  149. $.mockjax({
  150. url: '/test-api/users/123/avatar/',
  151. status: 200,
  152. responseText: {
  153. detail: "Gallery avataru set!",
  154. avatar_hash: 'n33wh44sh',
  155. options: apiResponse
  156. }
  157. });
  158. snackbarStore.callback(function(message) {
  159. assert.deepEqual(message, {
  160. message: "Gallery avataru set!",
  161. type: 'success'
  162. }, "valid message was shown");
  163. });
  164. let component = null;
  165. /* jshint ignore:start */
  166. let onComplete = function(avatarHash, options) {
  167. assert.equal(avatarHash, 'n33wh44sh', "new hash was passed to callback");
  168. assert.deepEqual(options, apiResponse, "new ops ware passed to callback");
  169. done();
  170. }
  171. component = testUtils.render(
  172. <ChangeAvatarGallery user={misago.get('user')}
  173. options={apiResponse}
  174. onComplete={onComplete} />
  175. );
  176. /* jshint ignore:end */
  177. testUtils.onElement('#test-mount .btn-avatar', function() {
  178. testUtils.simulateClick('#test-mount .btn-avatar');
  179. });
  180. testUtils.onElement('#test-mount .avatar-selected', function() {
  181. assert.equal(component.state.selection, "avatars/Nature/arctic_fox.jpg",
  182. "avatar selection callback works");
  183. testUtils.simulateClick('#test-mount .btn-primary');
  184. });
  185. });
  186. });
  187. describe("Avatar Gallery", function() {
  188. beforeEach(function() {
  189. misago._context = {
  190. MEDIA_URL: '/test-media/'
  191. };
  192. });
  193. afterEach(function() {
  194. testUtils.unmountComponents();
  195. });
  196. it("renders", function() {
  197. /* jshint ignore:start */
  198. testUtils.render(
  199. <Gallery name="Test gallery"
  200. images={apiResponse.galleries[0].images} />
  201. );
  202. /* jshint ignore:end */
  203. let element = $('#test-mount .avatars-gallery');
  204. assert.ok(element.length, "component renders");
  205. assert.equal(element.find('h3').text().trim(), "Test gallery",
  206. "gallery title is rendered");
  207. apiResponse.galleries[0].images.forEach(function(i) {
  208. assert.ok(element.find('button>img[src="/test-media/' + i + '"]').length,
  209. "component contains image");
  210. });
  211. });
  212. it("passess callback", function(done) { // jshint ignore:line
  213. /* jshint ignore:start */
  214. let select = function(image) {
  215. assert.equal(image, "avatars/Nature/arctic_fox.jpg",
  216. "callback was called with valid argument");
  217. done();
  218. };
  219. testUtils.render(
  220. <Gallery name="Test gallery"
  221. images={apiResponse.galleries[0].images}
  222. select={select} />
  223. );
  224. /* jshint ignore:end */
  225. testUtils.simulateClick("#test-mount button");
  226. });
  227. it("disables buttons", function() {
  228. /* jshint ignore:start */
  229. testUtils.render(
  230. <Gallery name="Test gallery"
  231. images={apiResponse.galleries[0].images}
  232. disabled={true} />
  233. );
  234. /* jshint ignore:end */
  235. apiResponse.galleries[0].images.forEach(function(i) {
  236. let image = $('#test-mount button>img[src="/test-media/' + i + '"]');
  237. assert.ok(image.parent().attr('disabled'), "has disabled attr");
  238. assert.ok(image.parent().hasClass('btn-disabled'), "has disabled class");
  239. });
  240. });
  241. });
  242. describe("Avatar Gallery Item", function() {
  243. beforeEach(function() {
  244. misago._context = {
  245. MEDIA_URL: '/test-media/'
  246. };
  247. });
  248. afterEach(function() {
  249. testUtils.unmountComponents();
  250. });
  251. it("renders", function() {
  252. /* jshint ignore:start */
  253. testUtils.render(
  254. <GalleryItem image="avatars/Nature/arctic_fox.jpg"
  255. disabled={false}
  256. selection={null} />
  257. );
  258. /* jshint ignore:end */
  259. let element = $('#test-mount button');
  260. assert.ok(element.length, "component renders");
  261. assert.ok(!element.hasClass('avatar-selected'), "item is not selected");
  262. assert.equal(
  263. element.find('img').attr('src'),
  264. '/test-media/avatars/Nature/arctic_fox.jpg',
  265. "component builds valid image url");
  266. });
  267. it("renders selected", function() {
  268. /* jshint ignore:start */
  269. testUtils.render(
  270. <GalleryItem image="avatars/Nature/arctic_fox.jpg"
  271. disabled={false}
  272. selection="avatars/Nature/arctic_fox.jpg" />
  273. );
  274. /* jshint ignore:end */
  275. let element = $('#test-mount button');
  276. assert.ok(element.length, "component renders");
  277. assert.ok(element.hasClass('avatar-selected'), "item is selected");
  278. });
  279. it("renders disabled", function() {
  280. /* jshint ignore:start */
  281. testUtils.render(
  282. <GalleryItem image="avatars/Nature/arctic_fox.jpg"
  283. disabled={true}
  284. selection="avatars/Nature/arctic_fox.jpg" />
  285. );
  286. /* jshint ignore:end */
  287. let element = $('#test-mount button');
  288. assert.ok(element.length, "component renders");
  289. assert.ok(element.attr('disabled'), "has disabled attr");
  290. assert.ok(element.hasClass('btn-disabled'), "has disabled class");
  291. assert.ok(element.hasClass('avatar-selected'), "item is selected");
  292. });
  293. it("executes callback", function(done) { // jshint ignore:line
  294. /* jshint ignore:start */
  295. let select = function(image) {
  296. assert.equal(image, "avatars/Nature/arctic_fox.jpg",
  297. "callback was called with valid argument");
  298. done();
  299. };
  300. testUtils.render(
  301. <GalleryItem image="avatars/Nature/arctic_fox.jpg"
  302. select={select} />
  303. );
  304. /* jshint ignore:end */
  305. testUtils.simulateClick('#test-mount button');
  306. });
  307. });