factory.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. 'use strict';
  2. require('jsdom-global')();
  3. const chai = require('chai');
  4. const dom = require('../mock/dom');
  5. const dataset = require('../mock/dataset');
  6. const mixitup = require('../../dist/mixitup.js');
  7. describe('mixitup()', () => {
  8. it('should throw an error if no container reference', () => {
  9. chai.assert.throws(() => mixitup(), Error, mixitup.messages.errorFactoryInvalidContainer());
  10. });
  11. it('should throw an error if a null container reference is passed', () => {
  12. chai.assert.throws(() => mixitup(null), Error, mixitup.messages.errorFactoryInvalidContainer());
  13. });
  14. it('should throw an error if an invalid container reference is passed', () => {
  15. chai.assert.throws(() => mixitup({}), Error, mixitup.messages.errorFactoryInvalidContainer());
  16. });
  17. it('should throw an error if an invalid reference or selector is passed', function() {
  18. chai.assert.throws(() => mixitup(false), Error, mixitup.messages.errorFactoryInvalidContainer());
  19. });
  20. it('should throw an error if an invalid configuration option is passed', function() {
  21. let container = dom.getContainer();
  22. chai.assert.throws(() => {
  23. mixitup(container, {
  24. animations: {}
  25. });
  26. }, TypeError);
  27. });
  28. it('should accept an element reference as a container', () => {
  29. let container = dom.getContainer();
  30. let mixer = mixitup(container);
  31. chai.assert.isOk(mixer);
  32. mixer.destroy();
  33. });
  34. it('should accept a container selector', () => {
  35. let frag = document.createDocumentFragment();
  36. let container = dom.getContainer();
  37. frag.appendChild(container);
  38. let mixer = mixitup('.mixitup-container', {}, frag);
  39. let state = mixer.getState();
  40. chai.assert.isOk(mixer);
  41. chai.assert.equal(state.container, frag.querySelector('.mixitup-container'));
  42. mixer.destroy();
  43. });
  44. it('should accept a container and valid configuration object', function() {
  45. let container = dom.getContainer();
  46. let mixer = mixitup(container, {
  47. selectors: {
  48. target: '[data-ref="mix"]'
  49. },
  50. controls: {
  51. enable: false
  52. }
  53. });
  54. let state = mixer.getState();
  55. chai.assert.isOk(mixer);
  56. chai.assert.equal(state.activeFilter.selector, '[data-ref="mix"]');
  57. mixer.destroy();
  58. });
  59. it('should throw an error if the container selector yields no element', () => {
  60. chai.assert.throws(() => mixitup('.invalid-container-selector'), Error, mixitup.messages.errorFactoryContainerNotFound());
  61. });
  62. it('should return an instance of a facade by default', () => {
  63. let container = dom.getContainer();
  64. let mixer = mixitup(container);
  65. chai.assert.instanceOf(mixer, mixitup.Facade);
  66. mixer.destroy();
  67. });
  68. it('should return an instance of a mixer if debug mode enabled', () => {
  69. let container = dom.getContainer();
  70. let mixer = mixitup(container, {
  71. debug: {
  72. enable: true
  73. },
  74. controls: {
  75. enable: false
  76. }
  77. });
  78. chai.assert.instanceOf(mixer, mixitup.Mixer);
  79. mixer.destroy();
  80. });
  81. it('should return a single instance of a mixer, wrapping the first element if multiple elements passed', () => {
  82. let elementList = [
  83. dom.getContainer(),
  84. dom.getContainer()
  85. ];
  86. let mixer = mixitup(elementList, {
  87. debug: {
  88. enable: true
  89. },
  90. controls: {
  91. enable: false
  92. }
  93. });
  94. chai.assert.instanceOf(mixer, mixitup.Mixer);
  95. chai.assert.equal(mixer.getState().container, elementList[0]);
  96. mixer.destroy();
  97. });
  98. it('should return an instance of a collection if multiple elements passed and `returnCollection` specified', () => {
  99. let elementList = [
  100. dom.getContainer(),
  101. dom.getContainer()
  102. ];
  103. let collection = mixitup(elementList, void(0), void(0), true);
  104. chai.assert.instanceOf(collection, mixitup.Collection);
  105. chai.assert.instanceOf(collection[0], mixitup.Facade);
  106. chai.assert.instanceOf(collection[1], mixitup.Facade);
  107. collection.mixitup('destroy');
  108. });
  109. it('should add a unique ID to the container if no ID present', () => {
  110. let container = dom.getContainer();
  111. let mixer = mixitup(container);
  112. let state = mixer.getState();
  113. chai.assert.equal(container.id, state.id);
  114. mixer.destroy();
  115. });
  116. it('should use any existing ID on the container as the mixer ID if present', () => {
  117. let container = dom.getContainer();
  118. let id = 'test-id';
  119. container.id = id;
  120. let mixer = mixitup(container);
  121. let state = mixer.getState();
  122. chai.assert.equal(id, state.id);
  123. mixer.destroy();
  124. });
  125. it('should not allow multiple instance to be instantiated on a single container', () => {
  126. let container = dom.getContainer();
  127. let mixer1 = mixitup(container, {
  128. debug: {
  129. enable: true
  130. },
  131. controls: {
  132. enable: false
  133. }
  134. });
  135. let mixer2 = mixitup(container, {
  136. debug: {
  137. enable: true,
  138. showWarnings: false
  139. },
  140. controls: {
  141. enable: false
  142. }
  143. });
  144. let facade = mixitup(container);
  145. chai.assert.equal(mixer1, mixer2);
  146. chai.assert.notEqual(facade, mixer1);
  147. chai.assert.notEqual(facade, mixer2);
  148. mixer1.destroy();
  149. });
  150. it('should respect a `load.filter` configuration option of none', () => {
  151. let container = dom.getContainer();
  152. let mixer = mixitup(container, {
  153. load: {
  154. filter: 'none'
  155. }
  156. });
  157. let state = mixer.getState();
  158. chai.assert.equal(state.activeFilter.selector, '');
  159. chai.assert.equal(state.totalShow, 0);
  160. chai.assert.equal(state.hide[0].style.display, 'none');
  161. mixer.destroy();
  162. });
  163. it('should respect a `load.filter` configuration option of a single selector', () => {
  164. let container = dom.getContainer();
  165. let mixer = mixitup(container, {
  166. load: {
  167. filter: '.category-a'
  168. }
  169. });
  170. let state = mixer.getState();
  171. chai.assert.equal(state.activeFilter.selector, '.category-a');
  172. chai.assert.equal(state.totalShow, 3);
  173. mixer.destroy();
  174. });
  175. it('should respect a `load.filter` configuration option of a compound selector', () => {
  176. let container = dom.getContainer();
  177. let mixer = mixitup(container, {
  178. load: {
  179. filter: '.category-a.category-c'
  180. }
  181. });
  182. let state = mixer.getState();
  183. chai.assert.equal(state.activeFilter.selector, '.category-a.category-c');
  184. chai.assert.equal(state.totalShow, 1);
  185. mixer.destroy();
  186. });
  187. it('should respect a `load.sort` configuration option', () => {
  188. let idsByPublishedDate = dataset.slice().sort((a, b) => {
  189. let dateA = a.published;
  190. let dateB = b.published;
  191. if (dateA < dateB) {
  192. return -1;
  193. }
  194. if (dateA > dateB) {
  195. return 1;
  196. }
  197. return 0;
  198. }).map(item => item.id.toString());
  199. let container = dom.getContainer();
  200. let mixer = mixitup(container, {
  201. load: {
  202. sort: 'published'
  203. }
  204. });
  205. let state = mixer.getState();
  206. let targetIds = state.show.map(el => el.id);
  207. chai.assert.deepEqual(targetIds, idsByPublishedDate);
  208. mixer.destroy();
  209. });
  210. it('should add a `layout.containerClassName` class if specified and be reflected in state', () => {
  211. let container = dom.getContainer();
  212. let mixer = mixitup(container, {
  213. layout: {
  214. containerClassName: 'grid'
  215. }
  216. });
  217. let state = mixer.getState();
  218. chai.assert.equal(state.activeContainerClassName, 'grid');
  219. mixer.destroy();
  220. });
  221. });