|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + */ |
| 8 | +'use strict'; |
| 9 | + |
| 10 | +describe('snapshot serializers', () => { |
| 11 | + it('works with first plugin', () => { |
| 12 | + const test = { |
| 13 | + foo: 1, |
| 14 | + }; |
| 15 | + expect(test).toMatchSnapshot(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('works with second plugin', () => { |
| 19 | + const test = { |
| 20 | + bar: 2, |
| 21 | + }; |
| 22 | + expect(test).toMatchSnapshot(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('works with nested serializable objects', () => { |
| 26 | + const test = { |
| 27 | + foo: { |
| 28 | + bar: 2, |
| 29 | + }, |
| 30 | + }; |
| 31 | + expect(test).toMatchSnapshot(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('works with default serializers', () => { |
| 35 | + const test = { |
| 36 | + $$typeof: Symbol.for('react.test.json'), |
| 37 | + children: null, |
| 38 | + props: { |
| 39 | + id: 'foo', |
| 40 | + }, |
| 41 | + type: 'div', |
| 42 | + }; |
| 43 | + expect(test).toMatchSnapshot(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('works with prepended plugins and default serializers', () => { |
| 47 | + const test = { |
| 48 | + $$typeof: Symbol.for('react.test.json'), |
| 49 | + children: null, |
| 50 | + props: { |
| 51 | + aProp: {a: 6}, |
| 52 | + bProp: {foo: 8}, |
| 53 | + }, |
| 54 | + type: 'div', |
| 55 | + }; |
| 56 | + expect(test).toMatchSnapshot(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('works with prepended plugins from expect method called once', () => { |
| 60 | + const test = { |
| 61 | + $$typeof: Symbol.for('react.test.json'), |
| 62 | + children: null, |
| 63 | + props: { |
| 64 | + aProp: {a: 6}, |
| 65 | + bProp: {foo: 8}, |
| 66 | + }, |
| 67 | + type: 'div', |
| 68 | + }; |
| 69 | + // Add plugin that overrides foo specified by Jest config in package.json |
| 70 | + expect.addSnapshotSerializer({ |
| 71 | + print: (val, serialize) => `Foo: ${serialize(val.foo)}`, |
| 72 | + test: val => val && val.hasOwnProperty('foo'), |
| 73 | + }); |
| 74 | + expect(test).toMatchSnapshot(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('works with prepended plugins from expect method called twice', () => { |
| 78 | + const test = { |
| 79 | + $$typeof: Symbol.for('react.test.json'), |
| 80 | + children: null, |
| 81 | + props: { |
| 82 | + aProp: {a: 6}, |
| 83 | + bProp: {foo: 8}, |
| 84 | + }, |
| 85 | + type: 'div', |
| 86 | + }; |
| 87 | + // Add plugin that overrides preceding added plugin |
| 88 | + expect.addSnapshotSerializer({ |
| 89 | + print: (val, serialize) => `FOO: ${serialize(val.foo)}`, |
| 90 | + test: val => val && val.hasOwnProperty('foo'), |
| 91 | + }); |
| 92 | + expect(test).toMatchSnapshot(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('works with array of strings in property matcher', () => { |
| 96 | + expect({ |
| 97 | + arrayOfStrings: ['stream'], |
| 98 | + }).toMatchSnapshot({ |
| 99 | + arrayOfStrings: ['stream'], |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('works with expect.XXX within array in property matcher', () => { |
| 104 | + expect({ |
| 105 | + arrayOfStrings: ['stream'], |
| 106 | + }).toMatchSnapshot({ |
| 107 | + arrayOfStrings: [expect.any(String)], |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments