Skip to content

Commit

Permalink
Addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyZhang777 committed Apr 23, 2024
1 parent 292e9a0 commit 16c6f98
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion static/js/formatters-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export function image(simpleOrComplexImage = {}, desiredSize = '200x', atLeastAs
urlPath = _removePhotoImageUrlExtension(urlPath);
}

const hostname = url.hostname.replace(/^[a-z]+\./, 'dyn.');
const hostname = url.hostname.replace(/^(a|dynl|dynm)\./, 'dyn.');
const formatOptionsString = _getImageFormatOptions(desiredSize, atLeastAsLarge, image.width, image.height);

return Object.assign(
Expand Down
3 changes: 1 addition & 2 deletions test-site/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ set_working_dir_to_test_site () {

set_working_dir_to_test_site

npx jambo build
chmod u+x ./public/static/node_modules/esbuild/bin/esbuild
npx jambo build && npx grunt webpack
npx grunt webpack
45 changes: 29 additions & 16 deletions tests/static/js/formatters-internal/image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Formatters from 'static/js/formatters.js';

describe('image formatter', () => {
const photoUrl = 'https://a.mktgcdn.com/p/FOO/2000x1000.jpg';
const photoUrl = 'https://dynm.mktgcdn.com/p/FOO/2000x1000.jpg';
const oldFileUrl = 'https://a.mktgcdn.com/f/0/FOO.jpg';
const newFileUrl = 'https://a.mktgcdn.com/f/FOO.jpg';
const euFileUrl = 'https://a.eu.mktgcdn.com/f/0/FOO.jpg';
Expand Down Expand Up @@ -30,7 +30,18 @@ describe('image formatter', () => {
height: 1000,
}

describe('when both dimensions are specified', () => {
describe('when both dimensions are specified and atLeastAsLarge is true', () => {
it('atLeastAsLarge is default to true if not provided by caller', () => {
const photoImgUrl = Formatters.image(photoImg, '1000x200').url;
expect(photoImgUrl).toEqual('https://dyn.mktgcdn.com/p/FOO/fit=contain,width=1000,height=500');
const oldFileImgUrl = Formatters.image(oldFileImg, '200x500').url;
expect(oldFileImgUrl).toEqual('https://dyn.mktgcdn.com/f/0/FOO.jpg/fit=contain,width=1000,height=500');
const newFileImgUrl = Formatters.image(newFileImg, '3000x1000').url;
expect(newFileImgUrl).toEqual('https://dyn.mktgcdn.com/f/FOO.jpg/fit=contain,width=3000,height=1500');
const euFileImgUrl = Formatters.image(euFileImg, '3000x4000').url;
expect(euFileImgUrl).toEqual('https://dyn.eu.mktgcdn.com/f/0/FOO.jpg/fit=contain,width=8000,height=4000');
});

it('if can get original ratio and atLeastAsLarge is true, preserve ratio and cover the specified space', () => {
const photoImgUrl = Formatters.image(photoImg, '1000x200', true).url;
expect(photoImgUrl).toEqual('https://dyn.mktgcdn.com/p/FOO/fit=contain,width=1000,height=500');
Expand All @@ -42,17 +53,6 @@ describe('image formatter', () => {
expect(euFileImgUrl).toEqual('https://dyn.eu.mktgcdn.com/f/0/FOO.jpg/fit=contain,width=8000,height=4000');
});

it('if can get original ratio and atLeastAsLarge is false, preserve ratio and return the largest image within the space', () => {
const photoImgUrl = Formatters.image(photoImg, '1000x200', false).url;
expect(photoImgUrl).toEqual('https://dyn.mktgcdn.com/p/FOO/fit=contain,width=1000,height=200');
const oldFileImgUrl = Formatters.image(oldFileImg, '200x500', false).url;
expect(oldFileImgUrl).toEqual('https://dyn.mktgcdn.com/f/0/FOO.jpg/fit=contain,width=200,height=500');
const newFileImgUrl = Formatters.image(newFileImg, '3000x1000', false).url;
expect(newFileImgUrl).toEqual('https://dyn.mktgcdn.com/f/FOO.jpg/fit=contain,width=3000,height=1000');
const euFileImgUrl = Formatters.image(euFileImg, '3000x4000', false).url;
expect(euFileImgUrl).toEqual('https://dyn.eu.mktgcdn.com/f/0/FOO.jpg/fit=contain,width=3000,height=4000');
});

it('if can\'t get original ratio and atLeastAsLarge is true, use desired dimensions', () => {
const photoImgUrl = Formatters.image({url: photoUrl}, '1000x200', true).url;
expect(photoImgUrl).toEqual('https://dyn.mktgcdn.com/p/FOO/fit=contain,width=1000,height=200');
Expand All @@ -63,8 +63,21 @@ describe('image formatter', () => {
const euFileImgUrl = Formatters.image({url: euFileUrl}, '3000x3000', true).url;
expect(euFileImgUrl).toEqual('https://dyn.eu.mktgcdn.com/f/0/FOO.jpg/fit=contain,width=3000,height=3000');
});
});

it('if can\'t get original ratio and atLeastAsLarge is true, use desired dimensions', () => {
describe('when both dimensions are specified and atLeastAsLarge is false', () => {
it('if can get original ratio and atLeastAsLarge is false, preserve ratio and return the largest image within the space', () => {
const photoImgUrl = Formatters.image(photoImg, '1000x200', false).url;
expect(photoImgUrl).toEqual('https://dyn.mktgcdn.com/p/FOO/fit=contain,width=1000,height=200');
const oldFileImgUrl = Formatters.image(oldFileImg, '200x500', false).url;
expect(oldFileImgUrl).toEqual('https://dyn.mktgcdn.com/f/0/FOO.jpg/fit=contain,width=200,height=500');
const newFileImgUrl = Formatters.image(newFileImg, '3000x1000', false).url;
expect(newFileImgUrl).toEqual('https://dyn.mktgcdn.com/f/FOO.jpg/fit=contain,width=3000,height=1000');
const euFileImgUrl = Formatters.image(euFileImg, '3000x4000', false).url;
expect(euFileImgUrl).toEqual('https://dyn.eu.mktgcdn.com/f/0/FOO.jpg/fit=contain,width=3000,height=4000');
});

it('if can\'t get original ratio and atLeastAsLarge is false, use desired dimensions', () => {
const photoImgUrl = Formatters.image({url: photoUrl}, '1000x200', false).url;
expect(photoImgUrl).toEqual('https://dyn.mktgcdn.com/p/FOO/fit=contain,width=1000,height=200');
const oldFileImgUrl = Formatters.image({url: oldFileUrl}, '200x500', false).url;
Expand Down Expand Up @@ -97,7 +110,7 @@ describe('image formatter', () => {
expect(euFileImgUrl).toEqual('https://dyn.eu.mktgcdn.com/f/0/FOO.jpg/fit=contain,width=3000');
});

it('Can restrict the dimensions by height, regardless of atLeastAsLarge', () => {
it('can restrict the dimensions by height, regardless of atLeastAsLarge', () => {
let photoImgUrl = Formatters.image(photoImg, 'x500', false).url;
expect(photoImgUrl).toEqual('https://dyn.mktgcdn.com/p/FOO/fit=contain,height=500');
let oldFileImgUrl = Formatters.image(oldFileImg, '1x500', false).url;
Expand All @@ -119,7 +132,7 @@ describe('image formatter', () => {
});

describe('when image has no dimensions specified', () => {
it('By default chooses the smallest image with width >= 200', () => {
it('by default chooses the smallest image with width >= 200', () => {
const photoImgUrl = Formatters.image(photoImg).url;
expect(photoImgUrl).toEqual('https://dyn.mktgcdn.com/p/FOO/fit=contain,width=200');
const oldFileImgUrl = Formatters.image(oldFileImg).url;
Expand Down

0 comments on commit 16c6f98

Please sign in to comment.