|
1 | 1 | import { gql } from 'apollo-server-core';
|
| 2 | +import * as fc from 'fast-check'; |
2 | 3 | import * as graphql from 'graphql';
|
3 |
| -import { execGraphQL } from './test.helpers'; |
| 4 | +import { |
| 5 | + coerceNullPrototypeObjects, |
| 6 | + execGraphQL, |
| 7 | + hasNullPrototype, |
| 8 | + hasObjectPrototype, |
| 9 | +} from './test.helpers'; |
| 10 | + |
| 11 | +/** For `fc.anything`: everything except null prototype objects. */ |
| 12 | +const withoutNullPrototype: fc.ObjectConstraints = { |
| 13 | + withBigInt: true, |
| 14 | + withBoxedValues: true, |
| 15 | + withDate: true, |
| 16 | + withMap: true, |
| 17 | + withObjectString: true, |
| 18 | + withNullPrototype: false, |
| 19 | + withSet: true, |
| 20 | + withTypedArray: true, |
| 21 | +}; |
| 22 | + |
| 23 | +describe('hasNullPrototype and hasObjectPrototype', () => { |
| 24 | + test('Object.create(null): hasNullPrototype, not hasObjectPrototype', () => { |
| 25 | + const o = Object.create(null) as { unknown: unknown }; |
| 26 | + expect(hasNullPrototype(o)).toBe(true); |
| 27 | + expect(hasObjectPrototype(o)).toBe(false); |
| 28 | + }); |
| 29 | + |
| 30 | + test('plain objects: hasObjectPrototype, not hasNullPrototype', () => { |
| 31 | + fc.assert( |
| 32 | + fc.property( |
| 33 | + fc.object({ ...withoutNullPrototype, withNullPrototype: true }), |
| 34 | + o => { |
| 35 | + expect(hasObjectPrototype(o)).toBe(true); |
| 36 | + expect(hasNullPrototype(o)).toBe(false); |
| 37 | + }, |
| 38 | + ), |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + test('everything else: not hasNullPrototype', () => { |
| 43 | + fc.assert( |
| 44 | + fc.property(fc.anything(withoutNullPrototype), o => { |
| 45 | + expect(hasNullPrototype(o)).toBe(false); |
| 46 | + }), |
| 47 | + ); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe('coerceNullPrototypeObjects', () => { |
| 52 | + test('everything excluding null prototype objects', () => { |
| 53 | + fc.assert( |
| 54 | + fc.property(fc.anything(withoutNullPrototype), (input: unknown) => { |
| 55 | + const result = coerceNullPrototypeObjects(input); |
| 56 | + expect(result).toStrictEqual(input); |
| 57 | + }), |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + test('everything including null prototype objects', () => { |
| 62 | + // Helper: True if any objects or arrays in o contain a null prototype object. |
| 63 | + const containsNullPrototype = (o: unknown) => |
| 64 | + hasNullPrototype(o) || |
| 65 | + (hasObjectPrototype(o) && Object.values(o).some(containsNullPrototype)) || |
| 66 | + (o instanceof Array && o.some(containsNullPrototype)); |
| 67 | + |
| 68 | + // Filter fc.anything() down to values that actually contain a null prototype object somewhere. |
| 69 | + const anythingContainingNullPrototypes = fc |
| 70 | + .anything({ |
| 71 | + ...withoutNullPrototype, |
| 72 | + withNullPrototype: true, |
| 73 | + }) |
| 74 | + .filter(containsNullPrototype); |
| 75 | + |
| 76 | + fc.assert( |
| 77 | + fc.property(anythingContainingNullPrototypes, input => { |
| 78 | + const output = coerceNullPrototypeObjects(input); |
| 79 | + // The input contains null prototypes, but the output should not. |
| 80 | + expect(containsNullPrototype(input)).toBe(true); |
| 81 | + expect(containsNullPrototype(output)).toBe(false); |
| 82 | + // The difference between input and output should fail toStrictEqual, |
| 83 | + // but still pass toEqual. |
| 84 | + expect(output).not.toStrictEqual(input); |
| 85 | + expect(output).toEqual(input); |
| 86 | + }), |
| 87 | + ); |
| 88 | + }); |
| 89 | +}); |
4 | 90 |
|
5 | 91 | describe('execGraphQL', () => {
|
6 | 92 | let schema: graphql.GraphQLSchema;
|
|
0 commit comments