Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Picture/Image Optimisations - The DPR Problem (Part 1/?) #3641

Merged
merged 22 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
49c21bf
Refactor picture component to resolve HDPI bug, add basic tests
OllysCoding Nov 1, 2021
00f0d26
Merge branch 'main' of github.com:guardian/dotcom-rendering into olly…
OllysCoding Nov 8, 2021
b7e3f5b
Fix side effects from using sort in getClosestSetForWidth, add tests …
OllysCoding Nov 9, 2021
4c5e125
Merge branch 'main' of github.com:guardian/dotcom-rendering into olly…
OllysCoding Nov 15, 2021
417e753
Update some image sizes to be more accurate
OllysCoding Nov 15, 2021
9aaefd4
Prettier fixes
OllysCoding Nov 15, 2021
3e41bc5
Add sources optimiser to reduce DOM size
OllysCoding Nov 15, 2021
869b920
Fix supporting image query
OllysCoding Nov 16, 2021
6f65754
Update comments
OllysCoding Nov 16, 2021
65000d1
Add support for edge case where there are no hdpi sources
OllysCoding Nov 16, 2021
3654b99
Fix MDPI media query, fix stype on mpdi source variable names
OllysCoding Nov 16, 2021
31ac992
comment typo fix
OllysCoding Nov 16, 2021
6a598a0
Use react fragment for fallback case whern there is no hdpi sources
OllysCoding Nov 16, 2021
3d02c97
Add Pixel as type alias to increase readability
mchv Nov 17, 2021
c412e58
Refactor Picture.tsx to make types more clear & defined, tidy up logi…
OllysCoding Nov 18, 2021
a6edaad
Round breakpont / 2 to avoid .5 in sizes attribute
OllysCoding Nov 18, 2021
085519e
WIP: Picture Documentation
OllysCoding Nov 19, 2021
90fb437
Update pictures documentation
OllysCoding Nov 19, 2021
88d2275
Merge branch 'main' of github.com:guardian/dotcom-rendering into olly…
OllysCoding Nov 19, 2021
b2f91e5
Update picture documentation with more accurate explanation of Source…
OllysCoding Nov 23, 2021
62e6ac4
Remove sizes attribute from sources as they`re redundant
OllysCoding Nov 23, 2021
1934ae6
Update documentation to remove sizes as they`re redundant
OllysCoding Nov 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions dotcom-rendering/src/web/components/Picture.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import { breakpoints } from '@guardian/src-foundations/mq';

import {
getClosestSetForWidth,
getDesiredWidth,
getSourcesFromSrcSets,
optimiseBreakpointSizes,
} from './Picture';

const hdpiSources: SrcSetItem[] = [
{
src: '1',
width: 1400,
},
{
src: '2',
width: 1240,
},
{
src: '3',
width: 930,
},
{
src: '4',
width: 1290,
},
];

const mdpiSources: SrcSetItem[] = [
{
src: '1',
width: 620,
},
{
src: '2',
width: 700,
},
{
src: '3',
width: 465,
},
{
src: '4',
width: 645,
},
];

/**
* mobile: 320
* mobileMedium: 375
* mobileLandscape: 480
* phablet: 660
* tablet: 740
* desktop: 980
* leftCol: 1140
* wide: 1300
*/

describe(`Picture`, () => {
describe('getClosestSetForWidth', () => {
it('Gets the closest source for a given width (hdpi)', () => {
// Breakpoints
expect(
getClosestSetForWidth(breakpoints.mobile * 2, hdpiSources)
.width,
).toBe(930);
expect(
getClosestSetForWidth(breakpoints.mobileMedium * 2, hdpiSources)
.width,
).toBe(930);
expect(
getClosestSetForWidth(
breakpoints.mobileLandscape * 2,
hdpiSources,
).width,
).toBe(1240);
expect(
getClosestSetForWidth(breakpoints.phablet * 2, hdpiSources)
.width,
).toBe(1400);
expect(
getClosestSetForWidth(breakpoints.tablet * 2, hdpiSources)
.width,
).toBe(1400);
expect(
getClosestSetForWidth(breakpoints.desktop * 2, hdpiSources)
.width,
).toBe(1400);
expect(
getClosestSetForWidth(breakpoints.leftCol * 2, hdpiSources)
.width,
).toBe(1400);
expect(
getClosestSetForWidth(breakpoints.wide * 2, hdpiSources).width,
).toBe(1400);

// Example widths
expect(getClosestSetForWidth(620 * 2, hdpiSources).width).toBe(
1240,
);
});

it('Gets the closest source for a given width (mdpi)', () => {
// Breakpoints
expect(
getClosestSetForWidth(breakpoints.mobile, mdpiSources).width,
).toBe(465);
expect(
getClosestSetForWidth(breakpoints.mobileMedium, mdpiSources)
.width,
).toBe(465);
expect(
getClosestSetForWidth(breakpoints.mobileLandscape, mdpiSources)
.width,
).toBe(620);
expect(
getClosestSetForWidth(breakpoints.phablet, mdpiSources).width,
).toBe(700);
expect(
getClosestSetForWidth(breakpoints.tablet, mdpiSources).width,
).toBe(700);
expect(
getClosestSetForWidth(breakpoints.desktop, mdpiSources).width,
).toBe(700);
expect(
getClosestSetForWidth(breakpoints.leftCol, mdpiSources).width,
).toBe(700);
expect(
getClosestSetForWidth(breakpoints.wide, mdpiSources).width,
).toBe(700);

// Example widths
expect(getClosestSetForWidth(620, mdpiSources).width).toBe(620);
});
});

describe('getDesiredWidth', () => {
it('Returns the correct with for HDPI', () => {
expect(getDesiredWidth(200, true)).toBe(400);
});
it('Returns the correct with for MDPI', () => {
expect(getDesiredWidth(200, false)).toBe(200);
});
});

describe('getSourcesFromSrcSets', () => {
it('Constructs the string correctly', () => {
expect(getSourcesFromSrcSets(hdpiSources)).toBe(
'1 1400w,2 1240w,3 930w,4 1290w',
);
expect(getSourcesFromSrcSets(mdpiSources)).toBe(
'1 620w,2 700w,3 465w,4 645w',
);
});
});

describe('optimiseBreakpointSizes', () => {
it('Leaves un-optimisable breakpointSizes as-is', () => {
const breakPointSizes: [number, number][] = [
[1000, 500],
[800, 400],
[600, 300],
[400, 200],
];
expect(optimiseBreakpointSizes(breakPointSizes)).toEqual(
breakPointSizes,
);
});

it('Correctly removes optimisable breakpointSizes', () => {
expect(
optimiseBreakpointSizes([
[1000, 500],
[800, 400],
[600, 400],
[400, 200],
]),
).toEqual([
[1000, 500],
[600, 400],
[400, 200],
]);

expect(
optimiseBreakpointSizes([
[1000, 500],
[800, 400],
[600, 200],
[400, 200],
]),
).toEqual([
[1000, 500],
[800, 400],
[400, 200],
]);

expect(
optimiseBreakpointSizes([
[1000, 500],
[800, 200],
[600, 200],
[400, 200],
]),
).toEqual([
[1000, 500],
[400, 200],
]);

expect(
optimiseBreakpointSizes([
[1000, 500],
[800, 500],
[600, 300],
[400, 200],
]),
).toEqual([
[800, 500],
[600, 300],
[400, 200],
]);
});
});
});
Loading