helpers.test.js 869 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { jsonDump } from '@/injected/web/util';
  2. test('jsonDump', () => {
  3. const sameChildObj = { foo: 1 };
  4. // eslint-disable-next-line no-restricted-syntax
  5. for (const obj of [
  6. 1,
  7. null,
  8. false,
  9. undefined,
  10. Infinity,
  11. NaN,
  12. 'abc',
  13. {},
  14. [],
  15. [1, 2, 3, undefined, , 4], // eslint-disable-line no-sparse-arrays
  16. {
  17. a: 1, b: '2', c: true, d: 'aaa',
  18. },
  19. {
  20. a: [1, 2, 3],
  21. b: { a: '\\"\x01foo\r\t"\u2028\u2029' },
  22. skipped: undefined,
  23. unsupported: new Set(),
  24. }, {
  25. sameChild1: sameChildObj,
  26. sameChild2: sameChildObj,
  27. sameChild3: [sameChildObj],
  28. },
  29. ]) {
  30. expect(jsonDump(obj)).toEqual(JSON.stringify(obj));
  31. }
  32. expect(() => {
  33. const cyclic = {};
  34. cyclic.foo = [1, 2, 3, { cyclic }];
  35. jsonDump(cyclic);
  36. }).toThrow(/Converting circular structure to JSON/);
  37. });