Skip to content

Commit 12e08b0

Browse files
committed
feat: Use CSS properties to define resize dimensions of images
1 parent 5129256 commit 12e08b0

File tree

7 files changed

+425
-217
lines changed

7 files changed

+425
-217
lines changed

projects/stream-chat-angular/src/lib/attachment-configuration.service.spec.ts

Lines changed: 122 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ describe('AttachmentConfigurationService', () => {
66
let service: AttachmentConfigurationService;
77

88
beforeEach(() => {
9+
spyOn(window, 'getComputedStyle').and.callFake(
10+
(htmlElement: Element) =>
11+
({
12+
getPropertyValue: (property) =>
13+
htmlElement[property as keyof Element] || '',
14+
} as CSSStyleDeclaration)
15+
);
916
service = TestBed.inject(AttachmentConfigurationService);
1017
});
1118

@@ -14,9 +21,14 @@ describe('AttachmentConfigurationService', () => {
1421
img_url: 'http://url/to/img',
1522
thumb_url: 'different/url',
1623
};
24+
const htmlElement = {
25+
'max-width': '300px',
26+
'max-height': '300px',
27+
height: '',
28+
} as any as HTMLElement;
1729

1830
expect(
19-
service.getImageAttachmentConfiguration(attachment, 'single')
31+
service.getImageAttachmentConfiguration(attachment, 'single', htmlElement)
2032
).toEqual({
2133
url: 'http://url/to/img?h=600&w=600',
2234
height: '300px',
@@ -28,7 +40,7 @@ describe('AttachmentConfigurationService', () => {
2840
};
2941

3042
expect(
31-
service.getImageAttachmentConfiguration(attachment, 'single')
43+
service.getImageAttachmentConfiguration(attachment, 'single', htmlElement)
3244
).toEqual({
3345
url: 'http://url/to/img?h=600&w=600',
3446
height: '300px',
@@ -40,7 +52,7 @@ describe('AttachmentConfigurationService', () => {
4052
};
4153

4254
expect(
43-
service.getImageAttachmentConfiguration(attachment, 'single')
55+
service.getImageAttachmentConfiguration(attachment, 'single', htmlElement)
4456
).toEqual({
4557
url: 'http://url/to/img?h=600&w=600',
4658
height: '300px',
@@ -55,20 +67,36 @@ describe('AttachmentConfigurationService', () => {
5567
img_url: 'http://url/to/img',
5668
thumb_url: 'different/url',
5769
};
58-
service.getImageAttachmentConfiguration(attachment, 'carousel');
70+
const htmlElement = {
71+
'max-width': 'none',
72+
'max-height': 'none',
73+
} as any as HTMLElement;
74+
service.getImageAttachmentConfiguration(
75+
attachment,
76+
'carousel',
77+
htmlElement
78+
);
5979

60-
expect(spy).toHaveBeenCalledWith(attachment, 'carousel');
80+
expect(spy).toHaveBeenCalledWith(attachment, 'carousel', htmlElement);
6181
});
6282

6383
it('should provide the correct configuration for gallery image attachments', () => {
6484
const attachment: Attachment = {
6585
img_url: 'http://url/to/img',
6686
};
87+
const htmlElement = {
88+
'max-width': '300px',
89+
height: '300px',
90+
} as any as HTMLElement;
6791

6892
expect(
69-
service.getImageAttachmentConfiguration(attachment, 'gallery')
93+
service.getImageAttachmentConfiguration(
94+
attachment,
95+
'gallery',
96+
htmlElement
97+
)
7098
).toEqual({
71-
url: 'http://url/to/img?h=300&w=300',
99+
url: 'http://url/to/img?h=600&w=600',
72100
height: '',
73101
width: '',
74102
});
@@ -78,9 +106,17 @@ describe('AttachmentConfigurationService', () => {
78106
const attachment: Attachment = {
79107
img_url: 'http://url/to/img',
80108
};
109+
const htmlElement = {
110+
'max-width': 'none',
111+
'max-height': 'none',
112+
} as any as HTMLElement;
81113

82114
expect(
83-
service.getImageAttachmentConfiguration(attachment, 'carousel')
115+
service.getImageAttachmentConfiguration(
116+
attachment,
117+
'carousel',
118+
htmlElement
119+
)
84120
).toEqual({
85121
url: 'http://url/to/img',
86122
height: '',
@@ -93,11 +129,17 @@ describe('AttachmentConfigurationService', () => {
93129
asset_url: 'http://url/to/video',
94130
thumb_url: 'http://url/to/poster',
95131
};
132+
const htmlElement = {
133+
'max-width': '300px',
134+
'max-height': '300px',
135+
} as any as HTMLElement;
96136

97-
expect(service.getVideoAttachmentConfiguration(attachment)).toEqual({
137+
expect(
138+
service.getVideoAttachmentConfiguration(attachment, htmlElement)
139+
).toEqual({
98140
url: 'http://url/to/video',
99141
height: '300px',
100-
width: '100%',
142+
width: '',
101143
thumbUrl: 'http://url/to/poster?h=600&w=600',
102144
});
103145

@@ -106,21 +148,25 @@ describe('AttachmentConfigurationService', () => {
106148
thumb_url: 'http://url/to/poster?oh=1080&ow=1920',
107149
};
108150

109-
expect(service.getVideoAttachmentConfiguration(attachment)).toEqual({
151+
expect(
152+
service.getVideoAttachmentConfiguration(attachment, htmlElement)
153+
).toEqual({
110154
url: 'http://url/to/video',
111155
height: '169px',
112-
width: '100%',
113-
thumbUrl: 'http://url/to/poster?oh=1080&ow=1920&h=338&w=600',
156+
width: '',
157+
thumbUrl: 'http://url/to/poster?oh=1080&ow=1920&h=600&w=1066',
114158
});
115159

116160
attachment = {
117161
asset_url: 'http://url/to/video',
118162
};
119163

120-
expect(service.getVideoAttachmentConfiguration(attachment)).toEqual({
164+
expect(
165+
service.getVideoAttachmentConfiguration(attachment, htmlElement)
166+
).toEqual({
121167
url: 'http://url/to/video',
122168
height: '300px',
123-
width: '100%',
169+
width: '',
124170
thumbUrl: undefined,
125171
});
126172
});
@@ -132,9 +178,13 @@ describe('AttachmentConfigurationService', () => {
132178
img_url: 'http://url/to/video',
133179
thumb_url: 'different/url',
134180
};
135-
service.getVideoAttachmentConfiguration(attachment);
181+
const htmlElement = {
182+
'max-width': '300px',
183+
'max-height': '300px',
184+
} as any as HTMLElement;
185+
service.getVideoAttachmentConfiguration(attachment, htmlElement);
136186

137-
expect(spy).toHaveBeenCalledWith(attachment);
187+
expect(spy).toHaveBeenCalledWith(attachment, htmlElement);
138188
});
139189

140190
it('should provide the correct configuration for GIFs', () => {
@@ -228,25 +278,77 @@ describe('AttachmentConfigurationService', () => {
228278
const attachment = {
229279
img_url: 'http://url/to/img?ow=3534&oh=4417',
230280
};
281+
const htmlElement = {
282+
'max-width': '300px',
283+
height: '300px',
284+
} as any as HTMLElement;
231285

232286
const result = service.getImageAttachmentConfiguration(
233287
attachment,
234-
'gallery'
288+
'gallery',
289+
htmlElement
235290
);
236291

237-
expect(result.url).toContain('h=374&w=300');
292+
expect(result.url).toContain('h=750&w=600');
238293
});
239294

240295
it('should turn off thumbnail generation for video files', () => {
241296
service.shouldGenerateVideoThumbnail = false;
297+
const htmlElement = {
298+
'max-width': '300px',
299+
'max-height': '300px',
300+
} as any as HTMLElement;
242301

243302
const attachment: Attachment = {
244303
asset_url: 'http://url/to/video',
245304
thumb_url: 'http://url/to/poster',
246305
};
247306

248307
expect(
249-
service.getVideoAttachmentConfiguration(attachment).thumbUrl
308+
service.getVideoAttachmentConfiguration(attachment, htmlElement).thumbUrl
250309
).toBeUndefined();
251310
});
311+
312+
it('should use original dimensions if image is smaller than max-width and max-height', () => {
313+
const attachment: Attachment = {
314+
image_url: 'http://url/to/img?ow=200&oh=100',
315+
};
316+
const htmlElement = {
317+
'max-width': '300px',
318+
'max-height': '300px',
319+
} as any as HTMLElement;
320+
321+
expect(
322+
service.getImageAttachmentConfiguration(attachment, 'single', htmlElement)
323+
.height
324+
).toBe('100px');
325+
326+
attachment.image_url = attachment.image_url!.replace('ow=200', 'ow=400');
327+
328+
expect(
329+
service.getImageAttachmentConfiguration(attachment, 'single', htmlElement)
330+
.height
331+
).toBe('75px');
332+
});
333+
334+
it('should override existing "h" and "w" URL params', () => {
335+
const attachment: Attachment = {
336+
image_url: 'http://url/to/img?crop=*&h=*&oh=0&ow=0&resize=*&ro=0&w=*',
337+
};
338+
const htmlElement = {
339+
'max-width': '300px',
340+
'max-height': '300px',
341+
} as any as HTMLElement;
342+
343+
const url = service.getImageAttachmentConfiguration(
344+
attachment,
345+
'single',
346+
htmlElement
347+
).url;
348+
349+
expect(url).not.toContain('h=*');
350+
expect(url).not.toContain('w=*');
351+
expect(url).toContain('h=600');
352+
expect(url).toContain('w=600');
353+
});
252354
});

0 commit comments

Comments
 (0)