8
0

areconnectedthroughproperties.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals window, document, Event */
  6. import areConnectedThroughProperties from '../../src/utils/areconnectedthroughproperties';
  7. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  8. describe( 'areConnectedThroughProperties()', () => {
  9. it( 'should return `false` if one of the value is primitive #1', () => {
  10. const el1 = [ 'foo' ];
  11. const el2 = 'foo';
  12. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
  13. } );
  14. it( 'should return `false` if one of the value is primitive #2', () => {
  15. const el1 = 0;
  16. const el2 = [ 0 ];
  17. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
  18. } );
  19. it( 'should return `false` if both of the values are primitives', () => {
  20. const el1 = null;
  21. const el2 = null;
  22. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
  23. } );
  24. it( 'should return `false` if both values are plain objects', () => {
  25. const el1 = {};
  26. const el2 = {};
  27. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
  28. } );
  29. it( 'should return `true` if both objects references to the same object', () => {
  30. const el1 = {};
  31. const el2 = el1;
  32. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
  33. } );
  34. it( 'should return `true` if both values share a common reference #1', () => {
  35. const foo = {};
  36. const el1 = { foo };
  37. const el2 = { foo };
  38. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
  39. } );
  40. it( 'should return `true` if both values share a common reference #2', () => {
  41. const foo = [];
  42. const el1 = [ foo ];
  43. const el2 = [ foo ];
  44. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
  45. } );
  46. it( 'should return `true` if the first structure is deep inside the second structure', () => {
  47. const el1 = {};
  48. const el2 = {
  49. foo: 1,
  50. bar: [ 1, 2, 3, new Map( [
  51. [ {}, new Set( [ 1, 2, 3 ] ) ],
  52. [ undefined, new Set( [
  53. Symbol( 'foo' ),
  54. null,
  55. { x: [ el1 ] }
  56. ] ) ]
  57. ] ) ]
  58. };
  59. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
  60. } );
  61. it( 'should return `true` if the second structure is deep inside the first structure', () => {
  62. const el2 = {};
  63. const el1 = {
  64. foo: 1,
  65. bar: [ 1, 2, 3, new Map( [
  66. [ {}, new Set( [ 1, 2, 3 ] ) ],
  67. [ undefined, new Set( [
  68. Symbol( 'foo' ),
  69. null,
  70. { x: [ el2 ] }
  71. ] ) ]
  72. ] ) ]
  73. };
  74. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
  75. } );
  76. it( 'should return `true` if both structures have a common reference', () => {
  77. const foo = {};
  78. const el1 = {
  79. foo: 1,
  80. bar: [ 1, 2, 3, new Map( [
  81. [ {}, new Set( [ 1, 2, 3 ] ) ],
  82. [ undefined, new Set( [
  83. Symbol( 'foo' ),
  84. null,
  85. { x: [ foo ] }
  86. ] ) ]
  87. ] ) ]
  88. };
  89. const el2 = {
  90. foo: 1,
  91. bar: [ 1, 2, 3, new Map( [
  92. [ {}, new Set( [ 1, 2, 3 ] ) ],
  93. [ undefined, new Set( [
  94. Symbol( 'foo' ),
  95. null,
  96. { x: [ foo ] }
  97. ] ) ]
  98. ] ) ]
  99. };
  100. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
  101. } );
  102. it( 'should return `false` if the structures is not connected #1', () => {
  103. const el1 = {};
  104. const el2 = {
  105. foo: 1,
  106. bar: [ 1, 2, 3, new Map( [
  107. [ {}, new Set( [ 1, 2, 3 ] ) ],
  108. [ undefined, new Set( [
  109. Symbol( 'foo' ),
  110. null,
  111. { x: [] }
  112. ] ) ]
  113. ] ) ]
  114. };
  115. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
  116. } );
  117. it( 'should return `false` if the structures is not connected #2', () => {
  118. const el1 = {
  119. foo: 1,
  120. bar: [ 1, 2, 3, new Map( [
  121. [ {}, new Set( [ 1, 2, 3 ] ) ],
  122. [ undefined, new Set( [
  123. Symbol( 'foo' ),
  124. null,
  125. { x: [] }
  126. ] ) ]
  127. ] ) ]
  128. };
  129. const el2 = {
  130. foo: 1,
  131. bar: [ 1, 2, 3, new Map( [
  132. [ {}, new Set( [ 1, 2, 3 ] ) ],
  133. [ undefined, new Set( [
  134. Symbol( 'foo' ),
  135. null,
  136. { x: [] }
  137. ] ) ]
  138. ] ) ]
  139. };
  140. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
  141. } );
  142. it( 'should work well with nested objects #1', () => {
  143. const el1 = {};
  144. el1.foo = el1;
  145. const el2 = {};
  146. el2.foo = el2;
  147. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
  148. } );
  149. it( 'should work well with nested objects #2', () => {
  150. const el1 = {};
  151. el1.foo = el1;
  152. const el2 = {};
  153. el2.foo = {
  154. foo: el2,
  155. bar: el1
  156. };
  157. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
  158. } );
  159. it( 'should skip DOM objects', () => {
  160. const evt = new Event( 'click' );
  161. const el1 = { window, document, evt };
  162. const el2 = { window, document, evt };
  163. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
  164. } );
  165. it( 'should skip date and regexp objects', () => {
  166. const date = new Date();
  167. const regexp = /123/;
  168. const el1 = { date, regexp };
  169. const el2 = { date, regexp };
  170. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
  171. } );
  172. it( 'should skip excluded properties', () => {
  173. const shared = { foo: [] };
  174. const el1 = { shared };
  175. const el2 = { shared };
  176. expect( areConnectedThroughProperties( el1, el2, new Set( [ shared ] ) ) ).to.be.false;
  177. } );
  178. it( 'should skip excluded properties #2', () => {
  179. const shared = {};
  180. const sharedNotExcluded = {};
  181. const el1 = { shared, sharedNotExcluded };
  182. const el2 = { shared, sharedNotExcluded };
  183. expect( areConnectedThroughProperties( el1, el2, new Set( [ shared ] ) ) ).to.be.true;
  184. } );
  185. it( 'should skip the `defaultValue` key since its commonly shared between editors', () => {
  186. const shared = {};
  187. const el1 = { defaultValue: shared };
  188. const el2 = { defaultValue: shared };
  189. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
  190. } );
  191. it( 'should skip the `defaultValue` key since its commonly shared between editors #2', () => {
  192. const shared = {};
  193. const el1 = { defaultValue: shared, shared };
  194. const el2 = { defaultValue: shared, shared };
  195. expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
  196. } );
  197. describe( 'integration tests', () => {
  198. afterEach( () => {
  199. delete Editor.builtinPlugins;
  200. delete Editor.defaultConfig;
  201. } );
  202. it( 'should return false for two different editors', () => {
  203. const editor1 = new Editor( {} );
  204. const editor2 = new Editor( {} );
  205. expect( areConnectedThroughProperties( editor1, editor2 ) ).to.be.false;
  206. } );
  207. it( 'should return false for two different editors sharing builtin plugins', () => {
  208. class FakePlugin {}
  209. Editor.builtinPlugins = [ FakePlugin ];
  210. const editor1 = new Editor();
  211. const editor2 = new Editor();
  212. expect( areConnectedThroughProperties( editor1, editor2 ) ).to.be.false;
  213. } );
  214. it( 'should return false for two different editors inheriting default configuration', () => {
  215. Editor.defaultConfig = {
  216. foo: {
  217. bar: []
  218. }
  219. };
  220. const editor1 = new Editor();
  221. const editor2 = new Editor();
  222. expect( areConnectedThroughProperties( editor1, editor2 ) ).to.be.false;
  223. } );
  224. it( 'should return false for two different editors sharing builtin plugins', () => {
  225. Editor.builtinPlugins = [
  226. class Foo {}
  227. ];
  228. const editor1 = new Editor();
  229. const editor2 = new Editor();
  230. expect( areConnectedThroughProperties( editor1, editor2 ) ).to.be.false;
  231. } );
  232. } );
  233. } );