Skip to content

Commit

Permalink
test(repack): added tests for inlining assets in newer React Native v…
Browse files Browse the repository at this point in the history
…ersions
  • Loading branch information
jbroma authored and RafikiTiki committed Oct 9, 2023
1 parent 63b218b commit 80228cc
Showing 1 changed file with 98 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,73 +207,110 @@ describe('assetLoader', () => {
});

describe('should inline asset', () => {
it('without scales', async () => {
const { code } = await compileBundle(
'android',
{
'./index.js': "export { default } from './__fixtures__/logo.png';",
},
true
);
it.each([
{ reactNativeVersion: '<0.72', moduleExportSyntax: 'module.exports =' },
{ reactNativeVersion: '>=0.72', moduleExportSyntax: 'export default ' },
])(
'without scales - React Native $reactNativeVersion',
async ({ moduleExportSyntax }) => {
const { code } = await compileBundle(
'android',
{
'node_modules/react-native/Libraries/Utilities/PixelRatio.js': `${moduleExportSyntax} { get: () => 1 };`,
'./index.js': "export { default } from './__fixtures__/logo.png';",
},
true
);

const context: { Export?: { default: Record<string, any> } } = {};
vm.runInNewContext(code, context);
const context: { Export?: { default: Record<string, any> } } = {};
vm.runInNewContext(code, context);

const logo = (
await fs.promises.readFile(
path.join(__dirname, './__fixtures__/logo.png')
)
).toString('base64');
expect(context.Export?.default).toEqual({
uri: `data:image/png;base64,${logo}`,
width: 2292,
height: 393,
scale: 1,
});
});
const logo = (
await fs.promises.readFile(
path.join(__dirname, './__fixtures__/logo.png')
)
).toString('base64');
expect(context.Export?.default).toEqual({
uri: `data:image/png;base64,${logo}`,
width: 2292,
height: 393,
scale: 1,
});
}
);

it.each([
{ prefferedScale: 1 },
{ prefferedScale: 2 },
{ prefferedScale: 3 },
])('with scales', async ({ prefferedScale }) => {
const { code } = await compileBundle(
'android',
{
'node_modules/react-native/Libraries/Utilities/PixelRatio.js': `module.exports = { get: () => ${prefferedScale} };`,
'./index.js': "export { default } from './__fixtures__/star.png';",
},
true
);
{
prefferedScale: 1,
reactNativeVersion: '<0.72',
moduleExportSyntax: 'module.exports =',
},
{
prefferedScale: 2,
reactNativeVersion: '<0.72',
moduleExportSyntax: 'module.exports =',
},
{
prefferedScale: 3,
reactNativeVersion: '<0.72',
moduleExportSyntax: 'module.exports =',
},
{
prefferedScale: 1,
reactNativeVersion: '>=0.72',
moduleExportSyntax: 'export default ',
},
{
prefferedScale: 2,
reactNativeVersion: '>=0.72',
moduleExportSyntax: 'export default ',
},
{
prefferedScale: 3,
reactNativeVersion: '>=0.72',
moduleExportSyntax: 'export default ',
},
])(
'with scales ($prefferedScale) - React Native $reactNativeVersion',
async ({ prefferedScale, moduleExportSyntax }) => {
const { code } = await compileBundle(
'android',
{
'node_modules/react-native/Libraries/Utilities/PixelRatio.js': `${moduleExportSyntax} { get: () => ${prefferedScale} };`,
'./index.js': "export { default } from './__fixtures__/star.png';",
},
true
);

const context: { Export?: { default: Record<string, any> } } = {};
vm.runInNewContext(code, context);
const context: { Export?: { default: Record<string, any> } } = {};
vm.runInNewContext(code, context);

const logos = await Promise.all([
(
await fs.promises.readFile(
path.join(__dirname, './__fixtures__/[email protected]')
)
).toString('base64'),
(
await fs.promises.readFile(
path.join(__dirname, './__fixtures__/[email protected]')
)
).toString('base64'),
(
await fs.promises.readFile(
path.join(__dirname, './__fixtures__/[email protected]')
)
).toString('base64'),
]);
const logos = await Promise.all([
(
await fs.promises.readFile(
path.join(__dirname, './__fixtures__/[email protected]')
)
).toString('base64'),
(
await fs.promises.readFile(
path.join(__dirname, './__fixtures__/[email protected]')
)
).toString('base64'),
(
await fs.promises.readFile(
path.join(__dirname, './__fixtures__/[email protected]')
)
).toString('base64'),
]);

expect(context.Export?.default).toEqual({
uri: `data:image/png;base64,${logos[prefferedScale - 1]}`,
width: 286,
height: 272,
scale: prefferedScale,
});
});
expect(context.Export?.default).toEqual({
uri: `data:image/png;base64,${logos[prefferedScale - 1]}`,
width: 286,
height: 272,
scale: prefferedScale,
});
}
);
});

describe('should convert to remote-asset', () => {
Expand Down Expand Up @@ -306,7 +343,7 @@ describe('assetLoader', () => {
{ prefferedScale: 1 },
{ prefferedScale: 2 },
{ prefferedScale: 3 },
])('with scales', async ({ prefferedScale }) => {
])('with scales $prefferedScale', async ({ prefferedScale }) => {
const { code } = await compileBundle(
'ios', // platform doesn't matter for remote-assets
{
Expand Down

0 comments on commit 80228cc

Please sign in to comment.