gallery.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. 'api_url': {
  60. 'avatar': '/test-api/users/123/avatar/'
  61. }
  62. }
  63. };
  64. });
  65. afterEach(function() {
  66. testUtils.unmountComponents();
  67. testUtils.snackbarClear(snackbar);
  68. $.mockjax.clear();
  69. });
  70. it("renders", function(done) {
  71. /* jshint ignore:start */
  72. testUtils.render(
  73. <ChangeAvatarGallery user={misago.get('user')}
  74. options={apiResponse} />
  75. );
  76. /* jshint ignore:end */
  77. testUtils.onElement('#test-mount .modal-avatar-gallery', function() {
  78. assert.ok(true, "component renders");
  79. done();
  80. });
  81. });
  82. it("handles backend rejection", function(done) {
  83. $.mockjax({
  84. url: '/test-api/users/123/avatar/',
  85. status: 400,
  86. responseText: {
  87. detail: "Lol nope!"
  88. }
  89. });
  90. snackbarStore.callback(function(message) {
  91. assert.deepEqual(message, {
  92. message: "Lol nope!",
  93. type: 'error'
  94. }, "error message was shown");
  95. done();
  96. });
  97. let component = null;
  98. /* jshint ignore:start */
  99. component = testUtils.render(
  100. <ChangeAvatarGallery user={misago.get('user')}
  101. options={apiResponse} />
  102. );
  103. /* jshint ignore:end */
  104. testUtils.onElement('#test-mount .btn-avatar', function() {
  105. testUtils.simulateClick('#test-mount .btn-avatar');
  106. });
  107. testUtils.onElement('#test-mount .avatar-selected', function() {
  108. assert.equal(component.state.selection, "avatars/Nature/arctic_fox.jpg",
  109. "avatar selection callback works");
  110. testUtils.simulateClick('#test-mount .btn-primary');
  111. });
  112. });
  113. it("handles backend error", function(done) { // jshint ignore:line
  114. $.mockjax({
  115. url: '/test-api/users/123/avatar/',
  116. status: 403,
  117. responseText: {
  118. detail: "Avatar can't be changed at this time!"
  119. }
  120. });
  121. snackbarStore.callback(function(message) {
  122. assert.deepEqual(message, {
  123. message: "Lol nope!",
  124. type: 'error'
  125. }, "error message was shown");
  126. done();
  127. });
  128. let component = null;
  129. /* jshint ignore:start */
  130. let showError = function(error) {
  131. assert.equal(error.detail, "Avatar can't be changed at this time!",
  132. "callback was called with backend error message");
  133. done()
  134. };
  135. component = testUtils.render(
  136. <ChangeAvatarGallery user={misago.get('user')}
  137. options={apiResponse}
  138. showError={showError} />
  139. );
  140. /* jshint ignore:end */
  141. testUtils.onElement('#test-mount .btn-avatar', function() {
  142. testUtils.simulateClick('#test-mount .btn-avatar');
  143. });
  144. testUtils.onElement('#test-mount .avatar-selected', function() {
  145. assert.equal(component.state.selection, "avatars/Nature/arctic_fox.jpg",
  146. "avatar selection callback works");
  147. testUtils.simulateClick('#test-mount .btn-primary');
  148. });
  149. });
  150. it("selects and submits avatar", function(done) { // jshint ignore:line
  151. $.mockjax({
  152. url: '/test-api/users/123/avatar/',
  153. status: 200,
  154. responseText: {
  155. detail: "Gallery avataru set!",
  156. avatar_hash: 'n33wh44sh',
  157. options: apiResponse
  158. }
  159. });
  160. snackbarStore.callback(function(message) {
  161. assert.deepEqual(message, {
  162. message: "Gallery avataru set!",
  163. type: 'success'
  164. }, "valid message was shown");
  165. });
  166. let component = null;
  167. /* jshint ignore:start */
  168. let onComplete = function(avatarHash, options) {
  169. assert.equal(avatarHash, 'n33wh44sh', "new hash was passed to callback");
  170. assert.deepEqual(options, apiResponse, "new ops ware passed to callback");
  171. done();
  172. }
  173. component = testUtils.render(
  174. <ChangeAvatarGallery user={misago.get('user')}
  175. options={apiResponse}
  176. onComplete={onComplete} />
  177. );
  178. /* jshint ignore:end */
  179. testUtils.onElement('#test-mount .btn-avatar', function() {
  180. testUtils.simulateClick('#test-mount .btn-avatar');
  181. });
  182. testUtils.onElement('#test-mount .avatar-selected', function() {
  183. assert.equal(component.state.selection, "avatars/Nature/arctic_fox.jpg",
  184. "avatar selection callback works");
  185. testUtils.simulateClick('#test-mount .btn-primary');
  186. });
  187. });
  188. });
  189. describe("Avatar Gallery", function() {
  190. beforeEach(function() {
  191. misago._context = {
  192. MEDIA_URL: '/test-media/'
  193. };
  194. });
  195. afterEach(function() {
  196. testUtils.unmountComponents();
  197. });
  198. it("renders", function() {
  199. /* jshint ignore:start */
  200. testUtils.render(
  201. <Gallery name="Test gallery"
  202. images={apiResponse.galleries[0].images} />
  203. );
  204. /* jshint ignore:end */
  205. let element = $('#test-mount .avatars-gallery');
  206. assert.ok(element.length, "component renders");
  207. assert.equal(element.find('h3').text().trim(), "Test gallery",
  208. "gallery title is rendered");
  209. apiResponse.galleries[0].images.forEach(function(i) {
  210. assert.ok(element.find('button>img[src="/test-media/' + i + '"]').length,
  211. "component contains image");
  212. });
  213. });
  214. it("passess callback", function(done) { // jshint ignore:line
  215. /* jshint ignore:start */
  216. let select = function(image) {
  217. assert.equal(image, "avatars/Nature/arctic_fox.jpg",
  218. "callback was called with valid argument");
  219. done();
  220. };
  221. testUtils.render(
  222. <Gallery name="Test gallery"
  223. images={apiResponse.galleries[0].images}
  224. select={select} />
  225. );
  226. /* jshint ignore:end */
  227. testUtils.simulateClick("#test-mount button");
  228. });
  229. it("disables buttons", function() {
  230. /* jshint ignore:start */
  231. testUtils.render(
  232. <Gallery name="Test gallery"
  233. images={apiResponse.galleries[0].images}
  234. disabled={true} />
  235. );
  236. /* jshint ignore:end */
  237. apiResponse.galleries[0].images.forEach(function(i) {
  238. let image = $('#test-mount button>img[src="/test-media/' + i + '"]');
  239. assert.ok(image.parent().attr('disabled'), "has disabled attr");
  240. assert.ok(image.parent().hasClass('btn-disabled'), "has disabled class");
  241. });
  242. });
  243. });
  244. describe("Avatar Gallery Item", function() {
  245. beforeEach(function() {
  246. misago._context = {
  247. MEDIA_URL: '/test-media/'
  248. };
  249. });
  250. afterEach(function() {
  251. testUtils.unmountComponents();
  252. });
  253. it("renders", function() {
  254. /* jshint ignore:start */
  255. testUtils.render(
  256. <GalleryItem image="avatars/Nature/arctic_fox.jpg"
  257. disabled={false}
  258. selection={null} />
  259. );
  260. /* jshint ignore:end */
  261. let element = $('#test-mount button');
  262. assert.ok(element.length, "component renders");
  263. assert.ok(!element.hasClass('avatar-selected'), "item is not selected");
  264. assert.equal(
  265. element.find('img').attr('src'),
  266. '/test-media/avatars/Nature/arctic_fox.jpg',
  267. "component builds valid image url");
  268. });
  269. it("renders selected", function() {
  270. /* jshint ignore:start */
  271. testUtils.render(
  272. <GalleryItem image="avatars/Nature/arctic_fox.jpg"
  273. disabled={false}
  274. selection="avatars/Nature/arctic_fox.jpg" />
  275. );
  276. /* jshint ignore:end */
  277. let element = $('#test-mount button');
  278. assert.ok(element.length, "component renders");
  279. assert.ok(element.hasClass('avatar-selected'), "item is selected");
  280. });
  281. it("renders disabled", function() {
  282. /* jshint ignore:start */
  283. testUtils.render(
  284. <GalleryItem image="avatars/Nature/arctic_fox.jpg"
  285. disabled={true}
  286. selection="avatars/Nature/arctic_fox.jpg" />
  287. );
  288. /* jshint ignore:end */
  289. let element = $('#test-mount button');
  290. assert.ok(element.length, "component renders");
  291. assert.ok(element.attr('disabled'), "has disabled attr");
  292. assert.ok(element.hasClass('btn-disabled'), "has disabled class");
  293. assert.ok(element.hasClass('avatar-selected'), "item is selected");
  294. });
  295. it("executes callback", function(done) { // jshint ignore:line
  296. /* jshint ignore:start */
  297. let select = function(image) {
  298. assert.equal(image, "avatars/Nature/arctic_fox.jpg",
  299. "callback was called with valid argument");
  300. done();
  301. };
  302. testUtils.render(
  303. <GalleryItem image="avatars/Nature/arctic_fox.jpg"
  304. select={select} />
  305. );
  306. /* jshint ignore:end */
  307. testUtils.simulateClick('#test-mount button');
  308. });
  309. });