v-click-outside-x.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. import PACKAGE_JSON from 'RootDir/package';
  2. import * as ESM from 'src/index';
  3. import * as WEB from 'dist/v-click-outside-x';
  4. import * as MIN from 'dist/v-click-outside-x.min';
  5. const ESMREQ = require('src/index');
  6. const WEBREQ = require('dist/v-click-outside-x');
  7. const MINREQ = require('dist/v-click-outside-x.min');
  8. const namePrefix = 'vClickOutside';
  9. const methods = [
  10. {method: ESM, name: `${namePrefix} ESM`},
  11. {method: WEB, name: `${namePrefix} WEB`},
  12. {method: MIN, name: `${namePrefix} MIN`},
  13. {method: ESMREQ, name: `${namePrefix} ESMREQ`},
  14. {method: WEBREQ, name: `${namePrefix} WEBREQ`},
  15. {method: MINREQ, name: `${namePrefix} MINREQ`},
  16. {method: ESM, name: `${namePrefix} ESM no document`, noDoc: true},
  17. ];
  18. const doc = window.document;
  19. methods.forEach(({method, name, noDoc}) => {
  20. const {directive, install} = method;
  21. let calls = 1;
  22. describe(`${name}`, () => {
  23. beforeAll(() => {
  24. if (noDoc) {
  25. calls = 0;
  26. delete window.document;
  27. }
  28. });
  29. beforeEach(() => {
  30. doc.addEventListener = jest.fn();
  31. doc.removeEventListener = jest.fn();
  32. });
  33. afterEach(() => {
  34. doc.addEventListener = undefined;
  35. doc.removeEventListener = undefined;
  36. });
  37. describe('plugin', () => {
  38. it('the directive is an object', () => {
  39. expect.assertions(1);
  40. expect(directive).toBeInstanceOf(Object);
  41. });
  42. it('it has all hook functions available', () => {
  43. expect.assertions(2);
  44. ['bind', 'unbind'].forEach(functionName => {
  45. expect(directive[functionName]).toBeInstanceOf(Function);
  46. });
  47. });
  48. it('$_captureInstances is an empty Map', () => {
  49. expect.assertions(1);
  50. expect(Object.keys(directive.$_captureInstances)).toHaveLength(0);
  51. });
  52. it('$_nonCaptureInstances is an empty Map', () => {
  53. expect.assertions(1);
  54. expect(Object.keys(directive.$_nonCaptureInstances)).toHaveLength(0);
  55. });
  56. it('$_onCaptureEvent to be a function', () => {
  57. expect.assertions(1);
  58. expect(directive.$_onCaptureEvent).toBeInstanceOf(Function);
  59. });
  60. it('$_onNonCaptureEvent to be a function', () => {
  61. expect.assertions(1);
  62. expect(directive.$_onNonCaptureEvent).toBeInstanceOf(Function);
  63. });
  64. it('version to be a string', () => {
  65. expect.assertions(1);
  66. expect(typeof directive.version).toStrictEqual('string');
  67. });
  68. it('version to be as per package.json', () => {
  69. expect.assertions(1);
  70. expect(directive.version).toStrictEqual(PACKAGE_JSON.version);
  71. });
  72. it('version to be enumerable', () => {
  73. expect.assertions(1);
  74. expect(
  75. Object.prototype.propertyIsEnumerable.call(directive, 'version'),
  76. ).toBe(true);
  77. });
  78. it('install the directive into the vue instance', () => {
  79. expect.assertions(2);
  80. const vue = {
  81. directive: jest.fn(),
  82. };
  83. install(vue);
  84. expect(vue.directive).toHaveBeenCalledWith('click-outside', directive);
  85. expect(vue.directive).toHaveBeenCalledTimes(1);
  86. });
  87. });
  88. describe('directive', () => {
  89. describe('bind/unbind', () => {
  90. describe('bind exceptions', () => {
  91. it('throws an error if value is not a function', () => {
  92. expect.assertions(1);
  93. const div1 = doc.createElement('div');
  94. const bindWithNoFunction = () => directive.bind(div1, {});
  95. expect(bindWithNoFunction).toThrowErrorMatchingSnapshot();
  96. });
  97. });
  98. describe('single', () => {
  99. const div1 = doc.createElement('div');
  100. it('adds to the list and event listener', () => {
  101. expect.assertions(6);
  102. const eventHandler = jest.fn();
  103. directive.bind(div1, {value: eventHandler});
  104. expect(Object.keys(directive.$_nonCaptureInstances)).toHaveLength(
  105. 1,
  106. );
  107. expect(directive.$_nonCaptureInstances).toHaveProperty('click');
  108. const clickInstances = directive.$_nonCaptureInstances.click;
  109. expect(clickInstances).toBeInstanceOf(Array);
  110. expect(clickInstances).toHaveLength(1);
  111. expect(clickInstances.find(item => item.el === div1)).toBeDefined();
  112. expect(doc.addEventListener.mock.calls).toHaveLength(calls);
  113. });
  114. it('removes from the list and event listener', () => {
  115. expect.assertions(2);
  116. directive.unbind(div1);
  117. expect(Object.keys(directive.$_nonCaptureInstances)).toHaveLength(
  118. 0,
  119. );
  120. expect(doc.removeEventListener.mock.calls).toHaveLength(calls);
  121. });
  122. });
  123. describe('multiple', () => {
  124. const div1 = doc.createElement('div');
  125. const div2 = doc.createElement('div');
  126. it('adds to the list and event listener', () => {
  127. expect.assertions(7);
  128. const eventHandler1 = jest.fn();
  129. const eventHandler2 = jest.fn();
  130. directive.bind(div1, {value: eventHandler1});
  131. directive.bind(div2, {arg: 'click', value: eventHandler2});
  132. expect(Object.keys(directive.$_nonCaptureInstances)).toHaveLength(
  133. 1,
  134. );
  135. expect(directive.$_nonCaptureInstances).toHaveProperty('click');
  136. const clickInstances = directive.$_nonCaptureInstances.click;
  137. expect(clickInstances).toBeInstanceOf(Array);
  138. expect(clickInstances).toHaveLength(2);
  139. expect(clickInstances.find(item => item.el === div1)).toBeDefined();
  140. expect(clickInstances.find(item => item.el === div2)).toBeDefined();
  141. expect(doc.addEventListener.mock.calls).toHaveLength(calls);
  142. });
  143. it('removes from the list and the event listener', () => {
  144. expect.assertions(7);
  145. directive.unbind(div1);
  146. expect(Object.keys(directive.$_nonCaptureInstances)).toHaveLength(
  147. 1,
  148. );
  149. expect(directive.$_nonCaptureInstances).toHaveProperty('click');
  150. const clickInstances = directive.$_nonCaptureInstances.click;
  151. expect(clickInstances).toBeInstanceOf(Array);
  152. expect(clickInstances).toHaveLength(1);
  153. expect(clickInstances.find(item => item.el === div2)).toBeDefined();
  154. directive.unbind(div2);
  155. expect(Object.keys(directive.$_nonCaptureInstances)).toHaveLength(
  156. 0,
  157. );
  158. expect(doc.removeEventListener.mock.calls).toHaveLength(calls);
  159. });
  160. });
  161. describe('bind', () => {
  162. it('saves the instance binding and element', () => {
  163. expect.assertions(11);
  164. const div1 = doc.createElement('div');
  165. const div2 = doc.createElement('div');
  166. const div3 = doc.createElement('div');
  167. const eventHandler1 = jest.fn();
  168. const eventHandler2 = jest.fn();
  169. directive.bind(div1, {
  170. arg: 'pointerdown',
  171. modifiers: {capture: true},
  172. value: eventHandler1,
  173. });
  174. directive.bind(div2, {
  175. arg: 'pointerdown',
  176. modifiers: {stop: true},
  177. value: eventHandler2,
  178. });
  179. directive.bind(div3, {
  180. arg: 'pointerdown',
  181. modifiers: {prevent: true},
  182. value: eventHandler2,
  183. });
  184. expect(Object.keys(directive.$_captureInstances)).toHaveLength(1);
  185. expect(directive.$_captureInstances).toHaveProperty('pointerdown');
  186. const clickCaptureInstances =
  187. directive.$_captureInstances.pointerdown;
  188. expect(clickCaptureInstances).toBeInstanceOf(Array);
  189. expect(clickCaptureInstances).toHaveLength(1);
  190. expect(
  191. clickCaptureInstances.find(item => item.el === div1),
  192. ).toStrictEqual({
  193. binding: {
  194. arg: 'pointerdown',
  195. modifiers: {
  196. capture: true,
  197. prevent: false,
  198. stop: false,
  199. },
  200. value: eventHandler1,
  201. },
  202. el: div1,
  203. });
  204. expect(Object.keys(directive.$_nonCaptureInstances)).toHaveLength(
  205. 1,
  206. );
  207. expect(directive.$_nonCaptureInstances).toHaveProperty(
  208. 'pointerdown',
  209. );
  210. const clickNonCaptureInstances =
  211. directive.$_nonCaptureInstances.pointerdown;
  212. expect(clickNonCaptureInstances).toBeInstanceOf(Array);
  213. expect(clickNonCaptureInstances).toHaveLength(2);
  214. expect(
  215. clickNonCaptureInstances.find(item => item.el === div2),
  216. ).toStrictEqual({
  217. binding: {
  218. arg: 'pointerdown',
  219. modifiers: {
  220. capture: false,
  221. prevent: false,
  222. stop: true,
  223. },
  224. value: eventHandler2,
  225. },
  226. el: div1,
  227. });
  228. expect(
  229. clickNonCaptureInstances.find(item => item.el === div3),
  230. ).toStrictEqual({
  231. binding: {
  232. arg: 'pointerdown',
  233. modifiers: {
  234. capture: false,
  235. prevent: true,
  236. stop: false,
  237. },
  238. value: eventHandler2,
  239. },
  240. el: div1,
  241. });
  242. directive.unbind(div1);
  243. directive.unbind(div2);
  244. directive.unbind(div3);
  245. });
  246. });
  247. });
  248. describe('$_onCaptureEvent', () => {
  249. const div1 = doc.createElement('div');
  250. const span = doc.createElement('span');
  251. div1.appendChild(span);
  252. it('calls the callback if the element is not the same and does not contain the event target', () => {
  253. expect.assertions(10);
  254. const a = doc.createElement('a');
  255. const event = {
  256. preventDefault: jest.fn(),
  257. stopPropagation: jest.fn(),
  258. target: a,
  259. };
  260. const eventHandler1 = jest.fn();
  261. directive.bind(div1, {value: eventHandler1});
  262. directive.$_onNonCaptureEvent(event);
  263. expect(eventHandler1).toHaveBeenCalledWith(event);
  264. expect(eventHandler1.mock.instances).toHaveLength(1);
  265. expect(eventHandler1.mock.instances[0]).toBe(directive);
  266. expect(event.preventDefault).not.toHaveBeenCalled();
  267. expect(event.stopPropagation).not.toHaveBeenCalled();
  268. directive.unbind(div1);
  269. const eventHandler2 = jest.fn();
  270. directive.bind(div1, {
  271. arg: 'touchdown',
  272. modifiers: {capture: true, prevent: true, stop: true},
  273. value: eventHandler2,
  274. });
  275. directive.$_onCaptureEvent(event);
  276. expect(eventHandler2).toHaveBeenCalledWith(event);
  277. expect(eventHandler2.mock.instances).toHaveLength(1);
  278. expect(eventHandler2.mock.instances[0]).toBe(directive);
  279. expect(event.preventDefault).toHaveBeenCalled();
  280. expect(event.stopPropagation).toHaveBeenCalled();
  281. directive.unbind(div1);
  282. });
  283. it('does not execute the callback if the event target its the element from the instance', () => {
  284. expect.assertions(2);
  285. const event = {
  286. target: div1,
  287. };
  288. const eventHandler = jest.fn();
  289. directive.bind(div1, {value: eventHandler});
  290. directive.$_onNonCaptureEvent(event);
  291. expect(eventHandler).not.toHaveBeenCalled();
  292. expect(eventHandler.mock.instances).toHaveLength(0);
  293. directive.unbind(div1);
  294. });
  295. it('does not execute the callback if the event target is contained in the element from the instance', () => {
  296. expect.assertions(2);
  297. const event = {
  298. target: span,
  299. };
  300. const eventHandler = jest.fn();
  301. directive.bind(div1, {value: eventHandler});
  302. directive.$_onNonCaptureEvent(event);
  303. expect(eventHandler).not.toHaveBeenCalled();
  304. expect(eventHandler.mock.instances).toHaveLength(0);
  305. directive.unbind(div1);
  306. });
  307. });
  308. });
  309. });
  310. });