diff --git a/packages/react-native-codegen/src/parsers/consistency/__tests__/checkComponentSnaps-test.js b/packages/react-native-codegen/src/parsers/consistency/__tests__/checkComponentSnaps-test.js new file mode 100644 index 00000000000..17f921ebd61 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/consistency/__tests__/checkComponentSnaps-test.js @@ -0,0 +1,22 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @emails oncall+react_native + * @format + */ + +'use strict'; + +const {compareSnaps, compareTsArraySnaps} = require('../compareSnaps.js'); + +const flowFixtures = require('../../flow/components/__test_fixtures__/fixtures.js'); +const flowSnaps = require('../../../../src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap'); +const tsFixtures = require('../../typescript/components/__test_fixtures__/fixtures.js'); +const tsSnaps = require('../../../../src/parsers/typescript/components/__tests__/__snapshots__/typescript-component-parser-test.js.snap'); +const tsExtraCases = ['ARRAY2_PROP_TYPES_NO_EVENTS']; + +compareSnaps(flowFixtures, flowSnaps, [], tsFixtures, tsSnaps, tsExtraCases); +compareTsArraySnaps(tsSnaps, tsExtraCases); diff --git a/packages/react-native-codegen/src/parsers/consistency/__tests__/checkModuleSnaps-test.js b/packages/react-native-codegen/src/parsers/consistency/__tests__/checkModuleSnaps-test.js new file mode 100644 index 00000000000..fcfaa3ed36b --- /dev/null +++ b/packages/react-native-codegen/src/parsers/consistency/__tests__/checkModuleSnaps-test.js @@ -0,0 +1,27 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @emails oncall+react_native + * @format + */ + +'use strict'; + +const {compareSnaps, compareTsArraySnaps} = require('../compareSnaps.js'); + +const flowFixtures = require('../../flow/modules/__test_fixtures__/fixtures.js'); +const flowSnaps = require('../../../../src/parsers/flow/modules/__tests__/__snapshots__/module-parser-snapshot-test.js.snap'); +const tsFixtures = require('../../typescript/modules/__test_fixtures__/fixtures.js'); +const tsSnaps = require('../../../../src/parsers/typescript/modules/__tests__/__snapshots__/typescript-module-parser-snapshot-test.js.snap'); +const tsExtraCases = [ + 'NATIVE_MODULE_WITH_ARRAY2_WITH_ALIAS', + 'NATIVE_MODULE_WITH_ARRAY2_WITH_UNION_AND_TOUPLE', + 'NATIVE_MODULE_WITH_BASIC_ARRAY2', + 'NATIVE_MODULE_WITH_COMPLEX_ARRAY2', +]; + +compareSnaps(flowFixtures, flowSnaps, [], tsFixtures, tsSnaps, tsExtraCases); +compareTsArraySnaps(tsSnaps, tsExtraCases); diff --git a/packages/react-native-codegen/src/parsers/consistency/compareSnaps.js b/packages/react-native-codegen/src/parsers/consistency/compareSnaps.js new file mode 100644 index 00000000000..2ef09816a84 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/consistency/compareSnaps.js @@ -0,0 +1,76 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @emails oncall+react_native + * @format + */ + +'use strict'; + +function compareSnaps( + flowFixtures, + flowSnaps, + flowExtraCases, + tsFixtures, + tsSnaps, + tsExtraCases, +) { + const flowCases = Object.keys(flowFixtures).sort(); + const tsCases = Object.keys(tsFixtures).sort(); + const commonCases = flowCases.filter(name => tsCases.indexOf(name) !== -1); + + describe('RN Codegen Parsers', () => { + it('should not unintentionally contains test case for Flow but not for TypeScript', () => { + expect( + flowCases.filter(name => commonCases.indexOf(name) === -1), + ).toEqual(flowExtraCases); + }); + + it('should not unintentionally contains test case for TypeScript but not for Flow', () => { + expect(tsCases.filter(name => commonCases.indexOf(name) === -1)).toEqual( + tsExtraCases, + ); + }); + + for (const commonCase of commonCases) { + it(`should generate the same snap from Flow and TypeScript for fixture ${commonCase}`, () => { + expect( + flowSnaps[ + `RN Codegen Flow Parser can generate fixture ${commonCase}` + ], + ).toEqual( + tsSnaps[ + `RN Codegen TypeScript Parser can generate fixture ${commonCase}` + ], + ); + }); + } + }); +} + +function compareTsArraySnaps(tsSnaps, tsExtraCases) { + for (const array2Case of tsExtraCases.filter( + name => name.indexOf('ARRAY2') !== -1, + )) { + const arrayCase = array2Case.replace('ARRAY2', 'ARRAY'); + it(`should generate the same snap from fixture ${arrayCase} and ${array2Case}`, () => { + expect( + tsSnaps[ + `RN Codegen TypeScript Parser can generate fixture ${arrayCase}` + ], + ).toEqual( + tsSnaps[ + `RN Codegen TypeScript Parser can generate fixture ${array2Case}` + ], + ); + }); + } +} + +module.exports = { + compareSnaps, + compareTsArraySnaps, +};