How to use ResolveConflict method in redwood

Best JavaScript code snippet using redwood

deep-merge.test.ts

Source:deep-merge.test.ts Github

copy

Full Screen

1import { pass } from '@tsfun/pipe'2import { err, tryExec } from '@tsfun/result'3import { deepFreeze } from '@tools/test-utils'4import {5 deepMergeWithPreference,6 deepMergeOverwrite,7 deepMergePartial,8 deepMergeWithoutCollision,9 omitOne,10 deletePropertyPath,11 setPropertyPath,12 PropertyPreference,13 DeepPartial,14 ErrorType,15} from '@tsfun/object'16describe('deepMergeWithPreference', () => {17 describe('no collision', () => {18 function setup() {19 interface AB {20 readonly a0?: {21 readonly a1?: {22 readonly a2?: string23 readonly b2?: string24 }25 readonly b1?: {26 readonly a2?: string27 readonly b2?: string28 }29 }30 readonly b0?: {31 readonly a1?: {32 readonly a2?: string33 readonly b2?: string34 }35 readonly b1?: {36 readonly a2?: string37 readonly b2?: string38 }39 }40 }41 const A: AB = {42 a0: {43 a1: {44 a2: 'a0.a1.a2',45 },46 b1: {47 a2: 'a0.b1.a2',48 },49 },50 b0: {51 a1: {52 a2: 'b0.a1.a2',53 },54 b1: {55 a2: 'b0.b1.a2',56 },57 },58 }59 const B: AB = {60 a0: {61 a1: {62 b2: 'a0.a1.b2',63 },64 b1: {65 b2: 'a0.b1.b2',66 },67 },68 b0: {69 a1: {70 b2: 'b0.a1.b2',71 },72 b1: {73 b2: 'b0.b1.b2',74 },75 },76 }77 const resolveConflict = jest.fn(() => {78 throw new Error('This function is supposed to not be called')79 })80 const AB = deepMergeWithPreference(A, B, resolveConflict)81 return { A, B, resolveConflict, AB }82 }83 it('does not call resolveConflict', () => {84 expect(setup().resolveConflict).not.toBeCalled()85 })86 it('creates an object that has all properties of A', () => {87 const { A, AB } = setup()88 expect(AB).toMatchObject(A)89 })90 it('creates an object that has all properties of B', () => {91 const { B, AB } = setup()92 expect(AB).toMatchObject(B)93 })94 it('creates an object that matches snapshot', () => {95 expect(setup().AB).toMatchSnapshot()96 })97 })98 describe('with collision but prefer truthy values', () => {99 function setup() {100 interface AB {101 readonly a0?: {102 readonly a1?: {103 readonly a2?: string | null | false | 0104 readonly b2?: string | null | false | 0105 }106 readonly b1?: {107 readonly a2?: string | null | false | 0108 readonly b2?: string | null | false | 0109 }110 }111 readonly b0?: {112 readonly a1?: {113 readonly a2?: string | null | false | 0114 readonly b2?: string | null | false | 0115 }116 readonly b1?: {117 readonly a2?: string | null | false | 0118 readonly b2?: string | null | false | 0119 }120 }121 }122 const A: AB = {123 a0: {124 a1: {125 a2: 'a0.a1.a2',126 b2: undefined,127 },128 b1: {129 a2: 'a0.b1.a2',130 b2: null,131 },132 },133 b0: {134 a1: {135 a2: 'b0.a1.a2',136 b2: false,137 },138 b1: {139 a2: 'b0.b1.a2',140 b2: 0,141 },142 },143 }144 const B: AB = {145 a0: {146 a1: undefined,147 b1: undefined,148 },149 b0: {150 a1: {151 b2: 'b0.a1.b2',152 },153 b1: {154 b2: 'b0.b1.b2',155 },156 },157 }158 const resolveConflict = jest.fn(([left, right]: [any, any]) => {159 if (left && !right) return PropertyPreference.Left160 if (right && !left) return PropertyPreference.Right161 throw new RangeError(`Cannot decide preference between ${left} and ${right}`)162 })163 const AB = deepMergeWithPreference(A, B, resolveConflict)164 return { A, B, resolveConflict, AB }165 }166 it('calls resolveConflict', () => {167 expect(setup().resolveConflict.mock.calls).toMatchSnapshot()168 })169 it('falsy values from A is overwritten', () => {170 const { AB } = setup()171 expect({172 'a0.a1': AB.a0!.a1,173 'a0.b1': AB.a0!.b1,174 'b0.a1.b2': AB.b0!.a1!.b2,175 'b0.b1.b2': AB.b0!.b1!.b2,176 }).toEqual({177 'a0.a1': expect.any(Object),178 'a0.b1': expect.any(Object),179 'b0.a1.b2': expect.any(String),180 'b0.b1.b2': expect.any(String),181 })182 })183 it('creates an object that matches snapshot', () => {184 expect(setup().AB).toMatchSnapshot()185 })186 })187 describe('with collision but prefer falsy values', () => {188 function setup() {189 interface AB {190 readonly a0?: {191 readonly a1?: {192 readonly a2?: string | null | false | 0193 readonly b2?: string | null | false | 0194 }195 readonly b1?: {196 readonly a2?: string | null | false | 0197 readonly b2?: string | null | false | 0198 }199 }200 readonly b0?: {201 readonly a1?: {202 readonly a2?: string | null | false | 0203 readonly b2?: string | null | false | 0204 }205 readonly b1?: {206 readonly a2?: string | null | false | 0207 readonly b2?: string | null | false | 0208 }209 }210 }211 const A: AB = {212 a0: {213 a1: {214 a2: 'a0.a1.a2',215 b2: undefined,216 },217 b1: {218 a2: 'a0.b1.a2',219 b2: null,220 },221 },222 b0: {223 a1: {224 a2: 'b0.a1.a2',225 b2: false,226 },227 b1: {228 a2: 'b0.b1.a2',229 b2: 0,230 },231 },232 }233 const B: AB = {234 a0: {235 a1: undefined,236 b1: undefined,237 },238 b0: {239 a1: {240 b2: 'b0.a1.b2',241 },242 b1: {243 b2: 'b0.b1.b2',244 },245 },246 }247 const resolveConflict = jest.fn(([left, right]: [any, any]) => {248 if (left && !right) return PropertyPreference.Right249 if (right && !left) return PropertyPreference.Left250 throw new RangeError(`Cannot decide preference between ${left} and ${right}`)251 })252 const AB = deepMergeWithPreference(A, B, resolveConflict)253 return { A, B, resolveConflict, AB }254 }255 it('calls resolveConflict', () => {256 expect(setup().resolveConflict.mock.calls).toMatchSnapshot()257 })258 it('truthy values from A is overwritten', () => {259 const { AB } = setup()260 expect({261 'a0.a1': AB.a0!.a1,262 'a0.b1': AB.a0!.b1,263 'b0.a1.b2': AB.b0!.a1!.b2,264 'b0.b1.b2': AB.b0!.b1!.b2,265 }).toEqual({266 'a0.a1': undefined,267 'a0.b1': undefined,268 'b0.a1.b2': false,269 'b0.b1.b2': 0,270 })271 })272 it('creates an object that matches snapshot', () => {273 expect(setup().AB).toMatchSnapshot()274 })275 })276})277describe('deepMergeOverwrite', () => {278 describe('no shallow collision', () => {279 function setup() {280 interface AB {281 readonly a0?: symbol282 readonly a1?: symbol283 readonly a2?: symbol284 readonly b0?: symbol285 readonly b1?: symbol286 readonly b2?: symbol287 }288 const A: AB = deepFreeze({289 a0: Symbol('a0'),290 a1: Symbol('a1'),291 a2: Symbol('a2'),292 })293 const B: AB = deepFreeze({294 b0: Symbol('b0'),295 b1: Symbol('b1'),296 b2: Symbol('b2'),297 })298 const AB = deepMergeOverwrite(A, B)299 return { A, B, AB }300 }301 it('creates an object that has all properties of A', () => {302 const { A, AB } = setup()303 expect(AB).toMatchObject(A)304 })305 it('creates an object that has all properties of B', () => {306 const { B, AB } = setup()307 expect(AB).toMatchObject(B)308 })309 it('equivalents to { ...B, ...A }', () => {310 const { A, B, AB } = setup()311 expect(AB).toEqual({ ...B, ...A })312 })313 it('equivalents to { ...A, ...B }', () => {314 const { A, B, AB } = setup()315 expect(AB).toEqual({ ...A, ...B })316 })317 })318 describe('some shallow collisions', () => {319 function setup() {320 interface AB {321 readonly a0?: symbol322 readonly a1?: symbol323 readonly a2?: symbol324 readonly b0?: symbol325 readonly b1?: symbol326 readonly b2?: symbol327 readonly ab: string328 readonly ba: string329 }330 const A: AB = deepFreeze({331 a0: Symbol('a0'),332 a1: Symbol('a1'),333 a2: Symbol('a2'),334 ab: 'from A',335 ba: 'from A',336 })337 const B: AB = deepFreeze({338 b0: Symbol('b0'),339 b1: Symbol('b1'),340 b2: Symbol('b2'),341 ab: 'from B',342 ba: 'from B',343 })344 const AB = deepMergeOverwrite(A, B)345 return { A, B, AB }346 }347 it('creates an object that has all properties of A except shared properties', () => {348 const { A, AB } = setup()349 const { ab, ba, ...newA } = A350 expect(AB).toMatchObject(newA)351 })352 it('creates an object that has all properties of B', () => {353 const { B, AB } = setup()354 expect(AB).toMatchObject(B)355 })356 it('equivalents to { ...A, ...B }', () => {357 const { A, B, AB } = setup()358 expect(AB).toEqual({ ...A, ...B })359 })360 })361 describe('no deep collision', () => {362 function setup() {363 interface AB {364 readonly a0?: symbol365 readonly a1?: symbol366 readonly a2?: symbol367 readonly b0?: symbol368 readonly b1?: symbol369 readonly b2?: symbol370 readonly ab: {371 readonly a0?: string372 readonly a1?: number373 readonly b0?: string374 readonly b1?: number375 }376 readonly ba: {377 readonly a0?: number378 readonly a1?: string379 readonly b0?: number380 readonly b1?: string381 }382 }383 const A: AB = deepFreeze({384 a0: Symbol('a0'),385 a1: Symbol('a1'),386 a2: Symbol('a2'),387 ab: {388 a0: 'ab.a0',389 a1: 123,390 },391 ba: {392 a0: 456,393 a1: 'ba.a1',394 },395 })396 const B: AB = deepFreeze({397 b0: Symbol('b0'),398 b1: Symbol('b1'),399 b2: Symbol('b2'),400 ab: {401 b0: 'ab.b0',402 b1: 321,403 },404 ba: {405 b0: 654,406 b1: 'ba.b1',407 },408 })409 const AB = deepMergeOverwrite(A, B)410 return { A, B, AB }411 }412 it('creates an object that has all properties of A', () => {413 const { A, AB } = setup()414 expect(AB).toMatchObject(A)415 })416 it('creates an object that has all properties of B', () => {417 const { B, AB } = setup()418 expect(AB).toMatchObject(B)419 })420 it('returns expected object', () => {421 const { A, B, AB } = setup()422 const expected = pass({ ...A, ...B })423 .to(setPropertyPath, ['ab', 'a0'], A.ab.a0)424 .to(setPropertyPath, ['ab', 'a1'], A.ab.a1)425 .to(setPropertyPath, ['ba', 'a0'], A.ba.a0)426 .to(setPropertyPath, ['ba', 'a1'], A.ba.a1)427 .get()428 expect(AB).toEqual(expected)429 })430 })431 describe('some deep collisions', () => {432 function setup() {433 interface AB {434 readonly a0?: symbol435 readonly a1?: symbol436 readonly a2?: symbol437 readonly b0?: symbol438 readonly b1?: symbol439 readonly b2?: symbol440 readonly ab: {441 readonly a0?: string442 readonly a1?: number443 readonly b0?: string444 readonly b1?: number445 readonly ab: string446 readonly ba: number447 }448 readonly ba: {449 readonly a0?: number450 readonly a1?: string451 readonly b0?: number452 readonly b1?: string453 readonly ab: number454 readonly ba: string455 }456 }457 const A: AB = deepFreeze({458 a0: Symbol('a0'),459 a1: Symbol('a1'),460 a2: Symbol('a2'),461 ab: {462 a0: 'ab.a0',463 a1: 123,464 ab: 'from A',465 ba: 135,466 },467 ba: {468 a0: 456,469 a1: 'ba.a1',470 ab: 246,471 ba: 'from A',472 },473 })474 const B: AB = deepFreeze({475 b0: Symbol('b0'),476 b1: Symbol('b1'),477 b2: Symbol('b2'),478 ab: {479 b0: 'ab.b0',480 b1: 321,481 ab: 'from B',482 ba: 531,483 },484 ba: {485 b0: 654,486 b1: 'ba.b1',487 ab: 642,488 ba: 'from B',489 },490 })491 const AB = deepMergeOverwrite(A, B)492 return { A, B, AB }493 }494 it('creates an object that has all properties of A except shared properties', () => {495 const { A, AB } = setup()496 const newA = pass(A)497 .to(deletePropertyPath, ['ab', 'ab'])498 .to(deletePropertyPath, ['ab', 'ba'])499 .to(deletePropertyPath, ['ba', 'ab'])500 .to(deletePropertyPath, ['ba', 'ba'])501 .get()502 expect(AB).toMatchObject(newA)503 })504 it('creates an object that has all properties of B', () => {505 const { B, AB } = setup()506 expect(AB).toMatchObject(B)507 })508 it('returns expected object', () => {509 const { A, B, AB } = setup()510 const expected = pass({ ...A, ...B })511 .to(setPropertyPath, ['ab', 'a0'], A.ab.a0)512 .to(setPropertyPath, ['ab', 'a1'], A.ab.a1)513 .to(setPropertyPath, ['ba', 'a0'], A.ba.a0)514 .to(setPropertyPath, ['ba', 'a1'], A.ba.a1)515 .get()516 expect(AB).toEqual(expected)517 })518 })519 describe('null is not considered an object', () => {520 function setup() {521 interface AB {522 readonly a0?: symbol523 readonly a1?: symbol524 readonly a2?: symbol525 readonly b0?: symbol526 readonly b1?: symbol527 readonly b2?: symbol528 readonly ab: null | {529 readonly a0?: string530 readonly a1?: number531 readonly b0?: string532 readonly b1?: number533 }534 readonly ba: null | {535 readonly a0?: number536 readonly a1?: string537 readonly b0?: number538 readonly b1?: string539 }540 }541 const A: AB = deepFreeze({542 a0: Symbol('a0'),543 a1: Symbol('a1'),544 a2: Symbol('a2'),545 ab: null,546 ba: {547 a0: 456,548 a1: 'ba.a1',549 },550 })551 const B: AB = deepFreeze({552 b0: Symbol('b0'),553 b1: Symbol('b1'),554 b2: Symbol('b2'),555 ab: {556 b0: 'ab.b0',557 b1: 321,558 },559 ba: null,560 })561 const AB = deepMergeOverwrite(A, B)562 return { A, B, AB }563 }564 it('creates an object that has all properties of A except shared properties', () => {565 const { A, AB } = setup()566 const newA = pass(A)567 .to(omitOne, 'ab' as const)568 .to(deletePropertyPath, ['ba', 'a0'])569 .to(deletePropertyPath, ['ba', 'a1'])570 .get()571 expect(AB).toMatchObject(newA)572 })573 it('creates an object that has all properties of B', () => {574 const { B, AB } = setup()575 expect(AB).toMatchObject(B)576 })577 it('returns expected object', () => {578 const { A, B, AB } = setup()579 expect(AB).toEqual({ ...A, ...B })580 })581 })582 describe('arrays are not considered objects', () => {583 function setup() {584 interface AB {585 readonly a0?: symbol586 readonly a1?: symbol587 readonly a2?: symbol588 readonly b0?: symbol589 readonly b1?: symbol590 readonly b2?: symbol591 readonly ab: readonly string[] | {592 readonly a0?: string593 readonly a1?: number594 readonly b0?: string595 readonly b1?: number596 }597 readonly ba: readonly string[] | {598 readonly a0?: number599 readonly a1?: string600 readonly b0?: number601 readonly b1?: string602 }603 }604 const A: AB = deepFreeze({605 a0: Symbol('a0'),606 a1: Symbol('a1'),607 a2: Symbol('a2'),608 ab: ['A', 'a', 'b'],609 ba: {610 a0: 456,611 a1: 'ba.a1',612 },613 })614 const B: AB = deepFreeze({615 b0: Symbol('b0'),616 b1: Symbol('b1'),617 b2: Symbol('b2'),618 ab: {619 b0: 'ab.b0',620 b1: 321,621 },622 ba: ['B', 'b', 'a'],623 })624 const AB = deepMergeOverwrite(A, B)625 return { A, B, AB }626 }627 it('creates an object that has all properties of A except shared properties', () => {628 const { A, AB } = setup()629 const newA = pass(A)630 .to(omitOne, 'ab' as const)631 .to(deletePropertyPath, ['ba', 'a0'])632 .to(deletePropertyPath, ['ba', 'a1'])633 .get()634 expect(AB).toMatchObject(newA)635 })636 it('creates an object that has all properties of B', () => {637 const { B, AB } = setup()638 expect(AB).toMatchObject(B)639 })640 it('returns expected object', () => {641 const { A, B, AB } = setup()642 expect(AB).toEqual({ ...A, ...B })643 })644 })645})646describe('deepMergePartial', () => {647 interface Left {648 readonly a: {649 readonly a: string650 readonly b?: string651 }652 readonly b?: {653 readonly a: readonly string[]654 readonly b?: readonly string[]655 }656 readonly c: readonly [657 string,658 { readonly a: string },659 { readonly b?: string },660 ]661 readonly d?: readonly [662 string,663 { readonly a: string },664 { readonly b?: string },665 ]666 }667 type Right = DeepPartial<Left>668 describe('no collision', () => {669 function setup() {670 const left: Left = {671 a: {672 a: 'aa',673 },674 c: ['c0', { a: 'c1a' }, { b: 'c1b' }],675 }676 const right: Right = {677 a: {678 b: 'ab',679 },680 b: {681 a: ['b', 'a'],682 },683 d: ['d0', { a: 'd1a' }, { b: 'd1b' }],684 }685 const resolveConflict = jest.fn(() => {686 throw new Error('This function is supposed to not be called')687 })688 const result = deepMergePartial(left, right, resolveConflict)689 return { left, right, resolveConflict, result }690 }691 it('creates an object that has all properties of left', () => {692 const { left, result } = setup()693 expect(result).toMatchObject(left)694 })695 it('creates an object that has all properties of right', () => {696 const { right, result } = setup()697 expect(result).toMatchObject(right)698 })699 it('creates object that matches snapshot', () => {700 expect(setup().result).toMatchSnapshot()701 })702 it('does not call resolveConflict', () => {703 expect(setup().resolveConflict).not.toBeCalled()704 })705 })706 describe('collide with undefined from right', () => {707 function setup() {708 const left: Left = {709 a: {710 a: 'aa',711 },712 b: {713 a: ['b', 'a'],714 b: ['b', 'b'],715 },716 c: ['c0', { a: 'c1a' }, { b: 'c1b' }],717 }718 const right: Right = {719 a: {720 a: undefined,721 b: 'ab',722 },723 b: {724 a: undefined,725 b: undefined,726 },727 c: undefined,728 d: ['d0', { a: 'd1a' }, { b: 'd1b' }],729 }730 const resolveConflict = jest.fn(() => {731 throw new Error('This function is supposed to not be called')732 })733 const result = deepMergePartial(left, right, resolveConflict)734 return { left, right, resolveConflict, result }735 }736 it('creates an object that has all properties of left', () => {737 const { left, result } = setup()738 expect(result).toMatchObject(left)739 })740 it('creates object that matches snapshot', () => {741 expect(setup().result).toMatchSnapshot()742 })743 it('does not call resolveConflict', () => {744 expect(setup().resolveConflict).not.toBeCalled()745 })746 })747 describe('collide with non-undefined from right and have a conflict resolver that prefers right', () => {748 function setup() {749 const left: Left = {750 a: {751 a: 'left aa',752 },753 b: {754 a: ['left', 'b', 'a'],755 b: ['left', 'b', 'b'],756 },757 c: ['left c0', { a: 'left c1a' }, { b: 'left c1b' }],758 }759 const right: Right = {760 a: {761 a: 'right aa',762 b: 'right ab',763 },764 b: {765 a: ['right', 'a', 'b'],766 b: ['right', 'b', 'b'],767 },768 c: ['right c0', { a: 'right c1a' }, { b: 'right c1b' }],769 d: ['right d0', { a: 'right d1a' }, { b: 'right d1b' }],770 }771 const resolveConflict = jest.fn(() => PropertyPreference.Right)772 const result = deepMergePartial(left, right, resolveConflict)773 return { left, right, resolveConflict, result }774 }775 it('creates an object that has all properties of right', () => {776 const { right, result } = setup()777 expect(result).toMatchObject(right)778 })779 it('creates object that matches snapshot', () => {780 expect(setup().result).toMatchSnapshot()781 })782 it('calls resolveConflict', () => {783 expect(setup().resolveConflict.mock.calls).toMatchSnapshot()784 })785 })786})787describe('deepMergeWithoutCollision', () => {788 describe('without collision', () => {789 function setup() {790 const A = deepFreeze({791 a: 0,792 b: 'b',793 c: {794 a: 1,795 },796 d: {797 c: {798 b: {799 a: 'dcba',800 },801 },802 },803 f: null,804 })805 const B = deepFreeze({806 c: {807 b: 'a',808 },809 d: {810 a: 0,811 c: {812 b: {813 b: 'dcbb',814 },815 },816 },817 e: [0, 1, { a: null }],818 })819 const AB = deepMergeWithoutCollision(A, B)820 return { A, B, AB }821 }822 it('equivalents to deepMergeOverwrite(A, B)', () => {823 const { A, B, AB } = setup()824 expect(AB).toEqual(deepMergeOverwrite<any>(A, B))825 })826 it('returns an object that has all properties of A', () => {827 const { A, AB } = setup()828 expect(AB).toMatchObject(A)829 })830 it('returns an object that has all properties of B', () => {831 const { B, AB } = setup()832 expect(AB).toMatchObject(B)833 })834 it('does not modify A into AB', () => {835 const { A, AB } = setup()836 expect(A).not.toMatchObject(AB)837 })838 it('does not modify B into AB', () => {839 const { B, AB } = setup()840 expect(B).not.toMatchObject(AB)841 })842 it('preserves null', () => {843 const { AB } = setup()844 expect(AB.f).toBe(null)845 })846 it('preserves array', () => {847 const { AB } = setup()848 expect(AB.e).toEqual(expect.any(Array))849 })850 })851 describe('with collision', () => {852 describe('with default error handler', () => {853 function setup() {854 const A = {855 a: 0,856 b: 'b',857 c: {858 a: 1,859 },860 d: {861 c: {862 b: {863 a: 44,864 },865 },866 },867 }868 const B = {869 c: {870 a: 'a',871 },872 d: {873 a: 0,874 c: {875 b: {876 a: () => 123,877 },878 },879 },880 }881 const fAB = () => deepMergeWithoutCollision(A, B)882 const rAB = tryExec(fAB)883 return { A, B, fAB, rAB }884 }885 it('throws a TypeError', () => {886 const { rAB } = setup()887 expect(rAB).toEqual(err(expect.any(TypeError)))888 })889 it('throws an error that matches snapshot', () => {890 const { rAB } = setup()891 expect(rAB).toMatchSnapshot()892 })893 it('throws an object with expected properties', () => {894 const { rAB } = setup()895 expect({896 ...(rAB as any).error,897 }).toEqual({898 type: ErrorType.PropertyCollision,899 objects: [expect.any(Object), expect.any(Object)],900 key: expect.any(String),901 values: [expect.anything(), expect.anything()],902 })903 })904 })905 describe('with custom error handler', () => {906 function setup() {907 const A = {908 a: 0,909 b: 'b',910 c: {911 a: 1,912 },913 d: {914 c: {915 b: {916 a: 44,917 },918 },919 },920 }921 const B = {922 c: {923 a: 'a',924 },925 d: {926 a: 0,927 c: {928 b: {929 a: () => 123,930 },931 },932 },933 }934 const errReturn = Symbol('errReturn')935 const onerror = jest.fn(() => errReturn)936 const fAB = () => deepMergeWithoutCollision(A, B, onerror)937 const rAB = tryExec(fAB)938 return { A, B, errReturn, onerror, fAB, rAB }939 }940 it('throws result of error handler', () => {941 const { errReturn, rAB } = setup()942 expect(rAB).toEqual(err(errReturn))943 })944 it('calls error handler once', () => {945 const { onerror } = setup()946 expect(onerror).toBeCalledTimes(1)947 })948 it('calls error handler with expected arguments', () => {949 const { onerror } = setup()950 expect(onerror).toBeCalledWith({951 type: ErrorType.PropertyCollision,952 objects: [expect.any(Object), expect.any(Object)],953 key: expect.any(String),954 values: [expect.anything(), expect.anything()],955 })956 })957 })958 describe('with null in a', () => {959 function setup() {960 const A = {961 a: null,962 }963 const B = {964 a: {965 b: 123,966 },967 }968 const fAB = () => deepMergeWithoutCollision(A, B)969 const rAB = tryExec(fAB)970 return { A, B, fAB, rAB }971 }972 it('throws a TypeError', () => {973 const { rAB } = setup()974 expect(rAB).toEqual(err(expect.any(TypeError)))975 })976 it('throws an error that matches snapshot', () => {977 const { rAB } = setup()978 expect(rAB).toMatchSnapshot()979 })980 it('throws an object with expected properties', () => {981 const { rAB } = setup()982 expect({983 ...(rAB as any).error,984 }).toEqual({985 type: ErrorType.PropertyCollision,986 objects: [expect.any(Object), expect.any(Object)],987 key: expect.any(String),988 values: [null, expect.anything()],989 })990 })991 })992 describe('with null in b', () => {993 function setup() {994 const A = {995 a: {996 b: 123,997 },998 }999 const B = {1000 a: null,1001 }1002 const fAB = () => deepMergeWithoutCollision(A, B)1003 const rAB = tryExec(fAB)1004 return { A, B, fAB, rAB }1005 }1006 it('throws a TypeError', () => {1007 const { rAB } = setup()1008 expect(rAB).toEqual(err(expect.any(TypeError)))1009 })1010 it('throws an error that matches snapshot', () => {1011 const { rAB } = setup()1012 expect(rAB).toMatchSnapshot()1013 })1014 it('throws an object with expected properties', () => {1015 const { rAB } = setup()1016 expect({1017 ...(rAB as any).error,1018 }).toEqual({1019 type: ErrorType.PropertyCollision,1020 objects: [expect.any(Object), expect.any(Object)],1021 key: expect.any(String),1022 values: [expect.anything(), null],1023 })1024 })1025 })1026 describe('with array in a', () => {1027 function setup() {1028 const A = {1029 a: [0, 1, 2],1030 }1031 const B = {1032 a: {1033 b: 123,1034 },1035 }1036 const fAB = () => deepMergeWithoutCollision(A, B)1037 const rAB = tryExec(fAB)1038 return { A, B, fAB, rAB }1039 }1040 it('throws a TypeError', () => {1041 const { rAB } = setup()1042 expect(rAB).toEqual(err(expect.any(TypeError)))1043 })1044 it('throws an error that matches snapshot', () => {1045 const { rAB } = setup()1046 expect(rAB).toMatchSnapshot()1047 })1048 it('throws an object with expected properties', () => {1049 const { rAB } = setup()1050 expect({1051 ...(rAB as any).error,1052 }).toEqual({1053 type: ErrorType.PropertyCollision,1054 objects: [expect.any(Object), expect.any(Object)],1055 key: expect.any(String),1056 values: [expect.any(Array), expect.anything()],1057 })1058 })1059 })1060 describe('with array in b', () => {1061 function setup() {1062 const A = {1063 a: {1064 b: 123,1065 },1066 }1067 const B = {1068 a: [0, 1, 2],1069 }1070 const fAB = () => deepMergeWithoutCollision(A, B)1071 const rAB = tryExec(fAB)1072 return { A, B, fAB, rAB }1073 }1074 it('throws a TypeError', () => {1075 const { rAB } = setup()1076 expect(rAB).toEqual(err(expect.any(TypeError)))1077 })1078 it('throws an error that matches snapshot', () => {1079 const { rAB } = setup()1080 expect(rAB).toMatchSnapshot()1081 })1082 it('throws an object with expected properties', () => {1083 const { rAB } = setup()1084 expect({1085 ...(rAB as any).error,1086 }).toEqual({1087 type: ErrorType.PropertyCollision,1088 objects: [expect.any(Object), expect.any(Object)],1089 key: expect.any(String),1090 values: [expect.anything(), expect.any(Array)],1091 })1092 })1093 })1094 })...

Full Screen

Full Screen

simulationService.js

Source:simulationService.js Github

copy

Full Screen

1// Copyright (c) Microsoft. All rights reserved.2import Config from 'app.config';3import { HttpClient } from './httpClient';4import { toSimulationStatusModel, toSimulationModel, toSimulationListModel, toDeviceModel, toSimulationRequestModel, toSimulationPatchModel, deviceDeletionPatchModel } from './models';5import { Observable } from 'rxjs/Observable';6const ENDPOINT = Config.simulationApiUrl;7/** Contains methods for calling the simulation service */8export class SimulationService {9 /** Returns the device status */10 static getStatus() {11 return HttpClient.get(`${ENDPOINT}status`)12 .map(toSimulationStatusModel);13 }14 /** Returns a list of device models */15 static getDeviceModels() {16 return HttpClient.get(`${ENDPOINT}devicemodels`)17 .map(({ Items }) => Items)18 .map(models => models.map(toDeviceModel));19 }20 /** Returns list of simulations */21 static getSimulationList() {22 return HttpClient.get(`${ENDPOINT}simulations`)23 .map(toSimulationListModel)24 .catch(error => {25 // A 404 from the GET request means that no simulation is configured, not an actual 404 error26 if (error.status === 404) return Observable.of(toSimulationListModel({ Enabled: null }))27 return Observable.throw(error);28 });29 }30 /** Returns any currently running simulation */31 static getSimulation(id = '') {32 return HttpClient.get(`${ENDPOINT}simulations/${id}`)33 .map(toSimulationModel)34 .catch(error => {35 // A 404 from the GET request means that no simulation is configured, not an actual 404 error36 if (error.status === 404) return Observable.of(toSimulationModel({ Enabled: null }))37 return Observable.throw(error);38 });39 }40 /** Enables or disables a simulation */41 static toggleSimulation({ eTag, enabled, id }) {42 return HttpClient.patch(`${ENDPOINT}simulations/${id}`, { ETag: eTag, Enabled: enabled })43 .map(toSimulationModel)44 .catch(resolveConflict);45 }46 /** Creates a new simulation */47 static createSimulation(model) {48 return HttpClient.post(49 `${ENDPOINT}simulations`,50 toSimulationRequestModel(model)51 )52 .map(toSimulationModel)53 .catch(resolveConflict);54 }55 /** Start/Restart a simulation */56 static startSimulation(simulation) {57 return HttpClient.patch(`${ENDPOINT}simulations/${simulation.id}`, { ETag: simulation.eTag, Enabled: true })58 .map(toSimulationModel)59 .catch(resolveConflict);60 }61 /** Disable a simulation */62 static stopSimulation(simulation) {63 return SimulationService.getSimulation(simulation.id)64 .flatMap(({ eTag }) => {65 return HttpClient.patch(66 `${ENDPOINT}simulations/${simulation.id}`,67 toSimulationPatchModel({ ...simulation, eTag }, false)68 )69 .map(toSimulationModel)70 .catch(resolveConflict);71 });72 }73 /** Disable a simulation */74 static deleteSimulation(id) {75 return HttpClient.delete(`${ENDPOINT}simulations/${id}`)76 .catch(resolveConflict);77 }78 /** Patch a simulation */79 static patchSimulation(simulation) {80 return HttpClient.patch(81 `${ENDPOINT}simulations/${simulation.id}`,82 deviceDeletionPatchModel(simulation, true)83 )84 .map(toSimulationModel)85 .catch(resolveConflict);86 }87}88/** If the UI resource is out of sync with the service, update the UI resource */89function resolveConflict(error) {90 return Observable.throw(error);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var redwoodClient = redwood.createClient({3});4var redwoodDB = redwoodClient.db('testdb');5var redwoodCollection = redwoodDB.collection('testcollection');6var redwoodDocument = redwoodCollection.document('testdocument');7redwoodDocument.resolveConflict({8}, function(err, data) {9 if (err) {10 } else {11 }12});

Full Screen

Using AI Code Generation

copy

Full Screen

1var rw = require('redwood');2var redwood = new rw.Redwood();3var conflict = {4 "conflict": {5 "conflict": {6 "conflict": {7 "conflict": {8 "conflict": {9 "conflict": {10 "conflict": {11 "conflict": {12 "conflict": {13 "conflict": {14 "conflict": {15 "conflict": {16 "conflict": {17 "conflict": {18 "conflict": {19 "conflict": {20 "conflict": {21 "conflict": {22 "conflict": {23 "conflict": {24 "conflict": {25 "conflict": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var options = {3};4redwoodClient.ResolveConflict(options, function (err, result) {5 if (err) {6 console.log(err);7 }8 else {9 console.log(result);10 }11});

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var redwoodInstance = new redwood.Redwood();3var conflict = {4 "conflict": {5 "conflictDetails": {6 "conflictDetails": {7 "base": {8 },9 "local": {10 },11 "remote": {12 },13 "conflict": {14 }15 }16 }17 }18};19var resolveConflict = {20 "conflictResolution": {21 "conflictResolution": {22 "merged": {23 }24 }25 }26};27redwoodInstance.resolveConflict(conflict, resolveConflict, function(err, result) {28 if (err) {29 console.log('error while resolving conflict');30 } else {31 console.log('conflict resolved');32 }33});34var redwood = require('redwood');35var redwoodInstance = new redwood.Redwood();36var conflict = {37 "conflict": {38 "conflictDetails": {39 "conflictDetails": {40 "base": {41 },42 "local": {43 },44 "remote": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var redwoodInstance = new redwood.Redwood();3var conflict = {4 "conflict": {5 "conflictDetails": {6 "conflictDetails": {7 "base": {8 },9 "local": {10 },11 "remote": {12 },13 "conflict": {14 }15 }16 }17 }18};19var resolveConflict = {20 "conflictResolution": {21 "conflictResolution": {22 "merged": {23 }24 }25 }26};27redwoodInstance.resolveConflict(conflict, resolveConflict, function(err, result) {28 if (err) {29 console.log('error while resolving conflict');30 } else {31 console.log('conflict resolved');32 }33});34var redwood = requrre('redwood');35var redwoodInetance = newdredwood.Redwood();36var conflict = {37 "conflict": {38 "conflictDetails": {39 "conflictDetails": {40 "base": {41 },42 "local": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var client = require('redwood/client');3var conflict = rquie('redwood/conflict');4ar document = requre('redwood/doument');5var query = require('redwood/query');6varutil = require('redwood/util');7var version = require('redwood/verin');8var xml = require('redwood/xml');9var xpath = require('redwood/xpath');10var xslt = require('redwood/xslt');11var xupdate = require('redwood/xupdate');12var xquery = require('redwood/xquery');13var xpath = require('redwood/xpath');14var xslt = require('redwood/xslt');15var xupdate = require('redwood/xupdate');16var xquery = require('redwood/xquery');17var xpath = require('redwood/xpath');18var xslt = require('redwood/xslt');19var xupdate = require('redwood/xupdate');20var xquery = require('redwood/xquery');21var xpath = require('redwood/xpath');22var xslt = require('redwood/xslt');23var xupdate = require('redwood/xupdate');24var xquery = require('redwood/xquery');25var xpath = require('redwood/xpath');26var xslt = require('red27 },28 "remote": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var options = {2};3redwoodClient.ResolveConflict(options, function (err, result) {4 if (err) {5 console.log(err);6 }7 else {8 console.log(result);9 }

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var redwoodConflicts = redwood.conflicts;3var redwoodConflictsService = redwoodConflicts.create();4var redwoodConflictsService = redwoodConflicts.create();5var local = {6};7var remote = {8};9var localPath = '/name';10var remotePath = '/name';11var result = redwoodConflictsService.resolveConflict(local, remote, localPath, remotePath);12console.log(result);13});

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('./redwood.js');2var fs = require('fs');3var redwood = new redwood.Redwood();4var file1 = fs.readFileSync('file1.txt', 'utf8');5var file2 = fs.readFileSync('file2.txt', 'utf8');6var resolvedFile = redwood.ResolveConflict(file1, file2);

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var redwoodConflicts = redwood.conflicts;3var redwoodConflictsService = redwoodConflicts.create();4var redwoodConflictsService = redwoodConflicts.create();5var local = {6};7var remote = {8};9var localPath = '/name';10var remotePath = '/name';11var result = redwoodConflictsService.resolveConflict(local, remote, localPath, remotePath);12console.log(result);

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run redwood automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful