project-data.spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {projectData} from './project-data.js';
  4. describe('projectData', function () {
  5. it('should require the parameter "schemaOrSource" to be a valid value', function () {
  6. const throwable = v => () => projectData(v, {});
  7. const error = s =>
  8. format(
  9. 'Projection schema must be an Object, a Function ' +
  10. 'or a non-empty String, but %s was given.',
  11. s,
  12. );
  13. expect(throwable('')).to.throw(error('""'));
  14. expect(throwable(10)).to.throw(error('10'));
  15. expect(throwable(0)).to.throw(error('0'));
  16. expect(throwable(true)).to.throw(error('true'));
  17. expect(throwable(false)).to.throw(error('false'));
  18. expect(throwable([])).to.throw(error('Array'));
  19. expect(throwable(null)).to.throw(error('null'));
  20. expect(throwable(undefined)).to.throw(error('undefined'));
  21. projectData({}, {});
  22. projectData(() => ({}), {});
  23. projectData('mySchema', {}, {resolver: () => ({})});
  24. });
  25. it('should require the parameter "options" to be an object', function () {
  26. const throwable = v => () => projectData({}, {}, v);
  27. const error = s =>
  28. format('Parameter "options" must be an Object, but %s was given.', s);
  29. expect(throwable('str')).to.throw(error('"str"'));
  30. expect(throwable('')).to.throw(error('""'));
  31. expect(throwable(10)).to.throw(error('10'));
  32. expect(throwable(0)).to.throw(error('0'));
  33. expect(throwable(true)).to.throw(error('true'));
  34. expect(throwable(false)).to.throw(error('false'));
  35. expect(throwable([])).to.throw(error('Array'));
  36. expect(throwable(null)).to.throw(error('null'));
  37. throwable({})();
  38. throwable(undefined)();
  39. });
  40. it('should require the option "strict" to be a boolean', function () {
  41. const throwable = v => () => projectData({}, {}, {strict: v});
  42. const error = s =>
  43. format('Option "strict" must be a Boolean, but %s was given.', s);
  44. expect(throwable('str')).to.throw(error('"str"'));
  45. expect(throwable('')).to.throw(error('""'));
  46. expect(throwable(10)).to.throw(error('10'));
  47. expect(throwable(0)).to.throw(error('0'));
  48. expect(throwable([])).to.throw(error('Array'));
  49. expect(throwable({})).to.throw(error('Object'));
  50. expect(throwable(null)).to.throw(error('null'));
  51. throwable(true)();
  52. throwable(false)();
  53. throwable(undefined)();
  54. });
  55. it('should require the option "scope" to be a non-empty string', function () {
  56. const throwable = v => () => projectData({}, {}, {scope: v});
  57. const error = s =>
  58. format('Option "scope" must be a non-empty String, but %s was given.', s);
  59. expect(throwable('')).to.throw(error('""'));
  60. expect(throwable(10)).to.throw(error('10'));
  61. expect(throwable(0)).to.throw(error('0'));
  62. expect(throwable(true)).to.throw(error('true'));
  63. expect(throwable(false)).to.throw(error('false'));
  64. expect(throwable([])).to.throw(error('Array'));
  65. expect(throwable({})).to.throw(error('Object'));
  66. expect(throwable(null)).to.throw(error('null'));
  67. throwable('str')();
  68. throwable(undefined)();
  69. });
  70. it('should require the option "resolver" to be a Function', function () {
  71. const throwable = v => () => projectData({}, {}, {resolver: v});
  72. const error = s =>
  73. format('Option "resolver" must be a Function, but %s was given.', s);
  74. expect(throwable('')).to.throw(error('""'));
  75. expect(throwable(10)).to.throw(error('10'));
  76. expect(throwable(0)).to.throw(error('0'));
  77. expect(throwable(true)).to.throw(error('true'));
  78. expect(throwable(false)).to.throw(error('false'));
  79. expect(throwable([])).to.throw(error('Array'));
  80. expect(throwable({})).to.throw(error('Object'));
  81. expect(throwable(null)).to.throw(error('null'));
  82. throwable(() => undefined)();
  83. throwable(undefined)();
  84. });
  85. it('should return a non-object and non-array data as is', function () {
  86. const fn = v => projectData({foo: true}, v);
  87. expect(fn('str')).to.be.eql('str');
  88. expect(fn('')).to.be.eql('');
  89. expect(fn(10)).to.be.eql(10);
  90. expect(fn(0)).to.be.eql(0);
  91. expect(fn(true)).to.be.eql(true);
  92. expect(fn(false)).to.be.eql(false);
  93. expect(fn(null)).to.be.eql(null);
  94. expect(fn(undefined)).to.be.eql(undefined);
  95. });
  96. it('should apply projection for each array element', function () {
  97. const schema = {foo: true, bar: false};
  98. const data = [
  99. {foo: 10, bar: 20, baz: 30},
  100. {foo: 40, bar: 50, baz: 60},
  101. ];
  102. const res = projectData(schema, data);
  103. expect(res).to.be.eql([
  104. {foo: 10, baz: 30},
  105. {foo: 40, baz: 60},
  106. ]);
  107. });
  108. it('should add fields without rules by default', function () {
  109. const res = projectData({}, {foo: 10, bar: 20});
  110. expect(res).to.be.eql({foo: 10, bar: 20});
  111. });
  112. it('should project fields by a boolean value', function () {
  113. const res = projectData({foo: true, bar: false}, {foo: 10, bar: 20});
  114. expect(res).to.be.eql({foo: 10});
  115. });
  116. it('should project fields by the select option', function () {
  117. const res = projectData(
  118. {foo: {select: true}, bar: {select: false}},
  119. {foo: 10, bar: 20},
  120. );
  121. expect(res).to.be.eql({foo: 10});
  122. });
  123. it('should ignore scope-related rules by default', function () {
  124. const res = projectData(
  125. {foo: {scopes: {input: false, output: false}}},
  126. {foo: 10},
  127. );
  128. expect(res).to.be.eql({foo: 10});
  129. });
  130. it('should create nested projection by the schema option', function () {
  131. const res = projectData(
  132. {foo: true, bar: false, qux: {schema: {abc: true, def: false}}},
  133. {foo: 10, bar: 20, qux: {abc: 30, def: 40}},
  134. );
  135. expect(res).to.be.eql({foo: 10, qux: {abc: 30}});
  136. });
  137. describe('schema factory', function () {
  138. it('should throw an error if the schema factory returns an invalid value', function () {
  139. const throwable = v => () => projectData(() => v, {});
  140. const error = s =>
  141. format(
  142. 'Projection schema factory must return an Object ' +
  143. 'or a non-empty String, but %s was given.',
  144. s,
  145. );
  146. expect(throwable('')).to.throw(error('""'));
  147. expect(throwable(10)).to.throw(error('10'));
  148. expect(throwable(0)).to.throw(error('0'));
  149. expect(throwable(true)).to.throw(error('true'));
  150. expect(throwable(false)).to.throw(error('false'));
  151. expect(throwable([])).to.throw(error('Array'));
  152. expect(throwable(null)).to.throw(error('null'));
  153. expect(throwable(undefined)).to.throw(error('undefined'));
  154. projectData(() => ({}), {});
  155. projectData(() => 'mySchema', {}, {resolver: () => ({})});
  156. });
  157. it('should resolve a schema object from the given factory', function () {
  158. let invoked = 0;
  159. const factory = () => {
  160. invoked++;
  161. return {foo: true, bar: false};
  162. };
  163. const res = projectData(factory, {foo: 10, bar: 20});
  164. expect(res).to.be.eql({foo: 10});
  165. expect(invoked).to.be.eq(1);
  166. });
  167. it('should use the schema factory in the nested schema', function () {
  168. let invoked = 0;
  169. const factory = () => {
  170. invoked++;
  171. return {baz: true, qux: false};
  172. };
  173. const res = projectData(
  174. {foo: true, bar: {schema: factory}},
  175. {foo: 10, bar: {baz: 20, qux: 30}},
  176. );
  177. expect(res).to.be.eql({foo: 10, bar: {baz: 20}});
  178. expect(invoked).to.be.eq(1);
  179. });
  180. });
  181. describe('named schema', function () {
  182. it('should throw an error if the schema resolver returns an invalid value', function () {
  183. const throwable = v => () =>
  184. projectData('mySchema', {}, {resolver: () => v});
  185. const error = s =>
  186. format(
  187. 'Projection schema resolver must return an Object, but %s was given.',
  188. s,
  189. );
  190. expect(throwable('str')).to.throw(error('"str"'));
  191. expect(throwable('')).to.throw(error('""'));
  192. expect(throwable(10)).to.throw(error('10'));
  193. expect(throwable(0)).to.throw(error('0'));
  194. expect(throwable(true)).to.throw(error('true'));
  195. expect(throwable(false)).to.throw(error('false'));
  196. expect(throwable([])).to.throw(error('Array'));
  197. expect(throwable(null)).to.throw(error('null'));
  198. expect(throwable(undefined)).to.throw(error('undefined'));
  199. throwable({})();
  200. });
  201. it('should throw an error if no schema resolver is provided when a schema name is given', function () {
  202. const throwable = () => projectData('mySchema', {});
  203. expect(throwable).to.throw(
  204. 'Unable to resolve the projection schema "mySchema" ' +
  205. 'without a provided resolver.',
  206. );
  207. });
  208. it('should pass the schema name to the schema resolver and project the given data', function () {
  209. let invoked = 0;
  210. const resolver = name => {
  211. expect(name).to.be.eq('mySchema');
  212. invoked++;
  213. return {foo: true, bar: false};
  214. };
  215. const res = projectData('mySchema', {foo: 10, bar: 20}, {resolver});
  216. expect(res).to.be.eql({foo: 10});
  217. expect(invoked).to.be.eq(1);
  218. });
  219. it('should use the schema resolver in the nested schema', function () {
  220. let invoked = 0;
  221. const resolver = name => {
  222. expect(name).to.be.eq('mySchema');
  223. invoked++;
  224. return {baz: true, qux: false};
  225. };
  226. const res = projectData(
  227. {foo: true, bar: {schema: 'mySchema'}},
  228. {foo: 10, bar: {baz: 20, qux: 30}},
  229. {resolver},
  230. );
  231. expect(res).to.be.eql({foo: 10, bar: {baz: 20}});
  232. expect(invoked).to.be.eq(1);
  233. });
  234. it('should resolve the schema name from the schema factory', function () {
  235. let invoked = 0;
  236. const resolver = name => {
  237. expect(name).to.be.eq('mySchema');
  238. invoked++;
  239. return {foo: true, bar: false};
  240. };
  241. const res = projectData(() => 'mySchema', {foo: 10, bar: 20}, {resolver});
  242. expect(res).to.be.eql({foo: 10});
  243. expect(invoked).to.be.eq(1);
  244. });
  245. });
  246. describe('strict mode', function () {
  247. it('should preserve fields not defined in the schema when the strict option is false', function () {
  248. const res = projectData({}, {foo: 10});
  249. expect(res).to.be.eql({foo: 10});
  250. });
  251. it('should remove fields without rules when the strict mode is enabled', function () {
  252. const res = projectData({}, {foo: 10}, {strict: true});
  253. expect(res).to.be.eql({});
  254. });
  255. it('should project fields by a boolean value', function () {
  256. const res = projectData(
  257. {foo: true, bar: false},
  258. {foo: 10, bar: 20},
  259. {strict: true},
  260. );
  261. expect(res).to.be.eql({foo: 10});
  262. });
  263. it('should project fields by the select option', function () {
  264. const res = projectData(
  265. {foo: {select: true}, bar: {select: false}},
  266. {foo: 10, bar: 20},
  267. {strict: true},
  268. );
  269. expect(res).to.be.eql({foo: 10});
  270. });
  271. it('should propagate the strict mode to nested schema', function () {
  272. const res = projectData(
  273. {foo: false, bar: {select: true, schema: {baz: true}}},
  274. {foo: 10, bar: {baz: 20, qux: 30}},
  275. {strict: true},
  276. );
  277. expect(res).to.be.eql({bar: {baz: 20}});
  278. });
  279. it('should ignore prototype properties', function () {
  280. const res = projectData(
  281. {bar: true, toString: true},
  282. {bar: 10},
  283. {strict: true},
  284. );
  285. expect(res).to.be.eql({bar: 10});
  286. });
  287. });
  288. describe('projection scope', function () {
  289. it('should apply scope-specific selection rule by a boolean value', function () {
  290. const schema = {
  291. foo: {
  292. select: false,
  293. scopes: {
  294. input: true,
  295. },
  296. },
  297. bar: true,
  298. };
  299. const data = {foo: 10, bar: 20};
  300. const res1 = projectData(schema, data);
  301. const res2 = projectData(schema, data, {scope: 'input'});
  302. expect(res1).to.be.eql({bar: 20});
  303. expect(res2).to.be.eql({foo: 10, bar: 20});
  304. });
  305. it('should apply scope-specific selection rule by the select option', function () {
  306. const schema = {
  307. foo: {
  308. select: false,
  309. scopes: {
  310. input: {select: true},
  311. },
  312. },
  313. bar: {select: true},
  314. };
  315. const data = {foo: 10, bar: 20};
  316. const res1 = projectData(schema, data);
  317. const res2 = projectData(schema, data, {scope: 'input'});
  318. expect(res1).to.be.eql({bar: 20});
  319. expect(res2).to.be.eql({foo: 10, bar: 20});
  320. });
  321. it('should fallback to general rule if scope rule is missing', function () {
  322. const schema = {
  323. foo: {
  324. select: true,
  325. scopes: {
  326. output: {select: false},
  327. },
  328. },
  329. };
  330. const data = {foo: 10};
  331. const res = projectData(schema, data, {scope: 'input'});
  332. expect(res).to.be.eql({foo: 10});
  333. });
  334. it('should fallback to the general rule if the scope options exists but lacks the select option', function () {
  335. const schema = {
  336. foo: {
  337. select: true,
  338. scopes: {
  339. input: {},
  340. },
  341. },
  342. bar: {
  343. select: false,
  344. scopes: {
  345. input: {},
  346. },
  347. },
  348. };
  349. const data = {foo: 10, bar: 20};
  350. const res = projectData(schema, data, {scope: 'input'});
  351. expect(res).to.be.eql({foo: 10});
  352. });
  353. });
  354. });