diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/features/admin/admin_appearance_spec.rb | 4 | ||||
-rw-r--r-- | spec/features/markdown_spec.rb | 2 | ||||
-rw-r--r-- | spec/features/uploads/user_uploads_avatar_to_group_spec.rb | 2 | ||||
-rw-r--r-- | spec/features/uploads/user_uploads_avatar_to_profile_spec.rb | 2 | ||||
-rw-r--r-- | spec/helpers/application_helper_spec.rb | 7 | ||||
-rw-r--r-- | spec/helpers/avatars_helper_spec.rb | 48 | ||||
-rw-r--r-- | spec/javascripts/lazy_loader_spec.js | 57 | ||||
-rw-r--r-- | spec/lib/banzai/filter/gollum_tags_filter_spec.rb | 4 | ||||
-rw-r--r-- | spec/lib/banzai/filter/image_lazy_load_filter_spec.rb | 19 | ||||
-rw-r--r-- | spec/support/matchers/markdown_matchers.rb | 4 |
10 files changed, 106 insertions, 43 deletions
diff --git a/spec/features/admin/admin_appearance_spec.rb b/spec/features/admin/admin_appearance_spec.rb index b9e361328df..2f90f668e89 100644 --- a/spec/features/admin/admin_appearance_spec.rb +++ b/spec/features/admin/admin_appearance_spec.rb @@ -63,11 +63,11 @@ feature 'Admin Appearance', feature: true do end def logo_selector - '//img[@src^="/uploads/-/system/appearance/logo"]' + '//img[data-src^="/uploads/-/system/appearance/logo"]' end def header_logo_selector - '//img[@src^="/uploads/-/system/appearance/header_logo"]' + '//img[data-src^="/uploads/-/system/appearance/header_logo"]' end def logo_fixture diff --git a/spec/features/markdown_spec.rb b/spec/features/markdown_spec.rb index 534be3ab5a7..1aca3e3a9fd 100644 --- a/spec/features/markdown_spec.rb +++ b/spec/features/markdown_spec.rb @@ -100,7 +100,7 @@ describe 'GitLab Markdown', feature: true do end it 'permits img elements' do - expect(doc).to have_selector('img[src*="smile.png"]') + expect(doc).to have_selector('img[data-src*="smile.png"]') end it 'permits br elements' do diff --git a/spec/features/uploads/user_uploads_avatar_to_group_spec.rb b/spec/features/uploads/user_uploads_avatar_to_group_spec.rb index 5843f18d89f..8188d4c79f4 100644 --- a/spec/features/uploads/user_uploads_avatar_to_group_spec.rb +++ b/spec/features/uploads/user_uploads_avatar_to_group_spec.rb @@ -18,7 +18,7 @@ feature 'User uploads avatar to group', feature: true do visit group_path(group) - expect(page).to have_selector(%Q(img[src$="/uploads/-/system/group/avatar/#{group.id}/dk.png"])) + expect(page).to have_selector(%Q(img[data-src$="/uploads/-/system/group/avatar/#{group.id}/dk.png"])) # Cheating here to verify something that isn't user-facing, but is important expect(group.reload.avatar.file).to exist diff --git a/spec/features/uploads/user_uploads_avatar_to_profile_spec.rb b/spec/features/uploads/user_uploads_avatar_to_profile_spec.rb index e8171dcaeb0..2628508afe8 100644 --- a/spec/features/uploads/user_uploads_avatar_to_profile_spec.rb +++ b/spec/features/uploads/user_uploads_avatar_to_profile_spec.rb @@ -16,7 +16,7 @@ feature 'User uploads avatar to profile', feature: true do visit user_path(user) - expect(page).to have_selector(%Q(img[src$="/uploads/-/system/user/avatar/#{user.id}/dk.png"])) + expect(page).to have_selector(%Q(img[data-src$="/uploads/-/system/user/avatar/#{user.id}/dk.png"])) # Cheating here to verify something that isn't user-facing, but is important expect(user.reload.avatar.file).to exist diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index f5e139685e8..ac5a58ac189 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -62,13 +62,13 @@ describe ApplicationHelper do avatar_url = "/uploads/-/system/project/avatar/#{project.id}/banana_sample.gif" expect(helper.project_icon(project.full_path).to_s) - .to eq "<img src=\"#{avatar_url}\" alt=\"Banana sample\" />" + .to eq "<img data-src=\"#{avatar_url}\" class=\" lazy\" src=\"#{LazyImageTagHelper.placeholder_image}\" />" allow(ActionController::Base).to receive(:asset_host).and_return(gitlab_host) avatar_url = "#{gitlab_host}/uploads/-/system/project/avatar/#{project.id}/banana_sample.gif" expect(helper.project_icon(project.full_path).to_s) - .to eq "<img src=\"#{avatar_url}\" alt=\"Banana sample\" />" + .to eq "<img data-src=\"#{avatar_url}\" class=\" lazy\" src=\"#{LazyImageTagHelper.placeholder_image}\" />" end it 'gives uploaded icon when present' do @@ -77,7 +77,8 @@ describe ApplicationHelper do allow_any_instance_of(Project).to receive(:avatar_in_git).and_return(true) avatar_url = "#{gitlab_host}#{project_avatar_path(project)}" - expect(helper.project_icon(project.full_path).to_s).to match(image_tag(avatar_url)) + expect(helper.project_icon(project.full_path).to_s) + .to eq "<img data-src=\"#{avatar_url}\" class=\" lazy\" src=\"#{LazyImageTagHelper.placeholder_image}\" />" end end diff --git a/spec/helpers/avatars_helper_spec.rb b/spec/helpers/avatars_helper_spec.rb index 049475a5408..d16fcf21e45 100644 --- a/spec/helpers/avatars_helper_spec.rb +++ b/spec/helpers/avatars_helper_spec.rb @@ -27,11 +27,11 @@ describe AvatarsHelper do it 'displays user avatar' do is_expected.to eq image_tag( - avatar_icon(user, 16), - class: 'avatar has-tooltip s16 ', + LazyImageTagHelper.placeholder_image, + class: 'avatar has-tooltip s16 lazy', alt: "#{user.name}'s avatar", title: user.name, - data: { container: 'body' } + data: { container: 'body', src: avatar_icon(user, 16) } ) end @@ -40,22 +40,8 @@ describe AvatarsHelper do it 'uses provided css_class' do is_expected.to eq image_tag( - avatar_icon(user, 16), - class: "avatar has-tooltip s16 #{options[:css_class]}", - alt: "#{user.name}'s avatar", - title: user.name, - data: { container: 'body' } - ) - end - end - - context 'with lazy parameter' do - let(:options) { { user: user, lazy: true } } - - it 'uses data-src instead of src' do - is_expected.to eq image_tag( - '', - class: 'avatar has-tooltip s16 ', + LazyImageTagHelper.placeholder_image, + class: "avatar has-tooltip s16 #{options[:css_class]} lazy", alt: "#{user.name}'s avatar", title: user.name, data: { container: 'body', src: avatar_icon(user, 16) } @@ -68,11 +54,11 @@ describe AvatarsHelper do it 'uses provided size' do is_expected.to eq image_tag( - avatar_icon(user, options[:size]), - class: "avatar has-tooltip s#{options[:size]} ", + LazyImageTagHelper.placeholder_image, + class: "avatar has-tooltip s#{options[:size]} lazy", alt: "#{user.name}'s avatar", title: user.name, - data: { container: 'body' } + data: { container: 'body', src: avatar_icon(user, options[:size]) } ) end end @@ -82,11 +68,11 @@ describe AvatarsHelper do it 'uses provided url' do is_expected.to eq image_tag( - options[:url], - class: 'avatar has-tooltip s16 ', + LazyImageTagHelper.placeholder_image, + class: 'avatar has-tooltip s16 lazy', alt: "#{user.name}'s avatar", title: user.name, - data: { container: 'body' } + data: { container: 'body', src: options[:url] } ) end end @@ -99,22 +85,22 @@ describe AvatarsHelper do it 'prefers user parameter' do is_expected.to eq image_tag( - avatar_icon(user, 16), - class: 'avatar has-tooltip s16 ', + LazyImageTagHelper.placeholder_image, + class: 'avatar has-tooltip s16 lazy', alt: "#{user.name}'s avatar", title: user.name, - data: { container: 'body' } + data: { container: 'body', src: avatar_icon(user, 16) } ) end end it 'uses user_name and user_email parameter if user is not present' do is_expected.to eq image_tag( - avatar_icon(options[:user_email], 16), - class: 'avatar has-tooltip s16 ', + LazyImageTagHelper.placeholder_image, + class: 'avatar has-tooltip s16 lazy', alt: "#{options[:user_name]}'s avatar", title: options[:user_name], - data: { container: 'body' } + data: { container: 'body', src: avatar_icon(options[:user_email], 16) } ) end end diff --git a/spec/javascripts/lazy_loader_spec.js b/spec/javascripts/lazy_loader_spec.js new file mode 100644 index 00000000000..1d81e4e2d1a --- /dev/null +++ b/spec/javascripts/lazy_loader_spec.js @@ -0,0 +1,57 @@ +import LazyLoader from '~/lazy_loader'; + +let lazyLoader = null; + +describe('LazyLoader', function () { + preloadFixtures('issues/issue_with_comment.html.raw'); + + beforeEach(function () { + loadFixtures('issues/issue_with_comment.html.raw'); + lazyLoader = new LazyLoader({ + observerNode: 'body', + }); + // Doing everything that happens normally in onload + lazyLoader.loadCheck(); + }); + describe('behavior', function () { + it('should copy value from data-src to src for img 1', function (done) { + const img = document.querySelectorAll('img[data-src]')[0]; + const originalDataSrc = img.getAttribute('data-src'); + img.scrollIntoView(); + + setTimeout(() => { + expect(img.getAttribute('src')).toBe(originalDataSrc); + expect(document.getElementsByClassName('js-lazy-loaded').length).toBeGreaterThan(0); + done(); + }, 100); + }); + + it('should lazy load dynamically added data-src images', function (done) { + const newImg = document.createElement('img'); + const testPath = '/img/testimg.png'; + newImg.className = 'lazy'; + newImg.setAttribute('data-src', testPath); + document.body.appendChild(newImg); + newImg.scrollIntoView(); + + setTimeout(() => { + expect(newImg.getAttribute('src')).toBe(testPath); + expect(document.getElementsByClassName('js-lazy-loaded').length).toBeGreaterThan(0); + done(); + }, 100); + }); + + it('should not alter normal images', function (done) { + const newImg = document.createElement('img'); + const testPath = '/img/testimg.png'; + newImg.setAttribute('src', testPath); + document.body.appendChild(newImg); + newImg.scrollIntoView(); + + setTimeout(() => { + expect(newImg).not.toHaveClass('js-lazy-loaded'); + done(); + }, 100); + }); + }); +}); diff --git a/spec/lib/banzai/filter/gollum_tags_filter_spec.rb b/spec/lib/banzai/filter/gollum_tags_filter_spec.rb index 082c0d4dd0d..cbb2808c6bb 100644 --- a/spec/lib/banzai/filter/gollum_tags_filter_spec.rb +++ b/spec/lib/banzai/filter/gollum_tags_filter_spec.rb @@ -22,7 +22,7 @@ describe Banzai::Filter::GollumTagsFilter, lib: true do tag = '[[images/image.jpg]]' doc = filter("See #{tag}", project_wiki: project_wiki) - expect(doc.at_css('img')['src']).to eq "#{project_wiki.wiki_base_path}/images/image.jpg" + expect(doc.at_css('img')['data-src']).to eq "#{project_wiki.wiki_base_path}/images/image.jpg" end it 'does not creates img tag if image does not exist' do @@ -40,7 +40,7 @@ describe Banzai::Filter::GollumTagsFilter, lib: true do tag = '[[http://example.com/image.jpg]]' doc = filter("See #{tag}", project_wiki: project_wiki) - expect(doc.at_css('img')['src']).to eq "http://example.com/image.jpg" + expect(doc.at_css('img')['data-src']).to eq "http://example.com/image.jpg" end it 'does not creates img tag for invalid URL' do diff --git a/spec/lib/banzai/filter/image_lazy_load_filter_spec.rb b/spec/lib/banzai/filter/image_lazy_load_filter_spec.rb new file mode 100644 index 00000000000..c19de7b784a --- /dev/null +++ b/spec/lib/banzai/filter/image_lazy_load_filter_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +describe Banzai::Filter::ImageLazyLoadFilter, lib: true do + include FilterSpecHelper + + def image(path) + %(<img src="#{path}" />) + end + + it 'transforms the image src to a data-src' do + doc = filter(image('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg')) + expect(doc.at_css('img')['data-src']).to eq '/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg' + end + + it 'works with external images' do + doc = filter(image('https://i.imgur.com/DfssX9C.jpg')) + expect(doc.at_css('img')['data-src']).to eq 'https://i.imgur.com/DfssX9C.jpg' + end +end diff --git a/spec/support/matchers/markdown_matchers.rb b/spec/support/matchers/markdown_matchers.rb index bbbbaf4c5e8..7afa57fb76b 100644 --- a/spec/support/matchers/markdown_matchers.rb +++ b/spec/support/matchers/markdown_matchers.rb @@ -17,7 +17,7 @@ module MarkdownMatchers image = actual.at_css('img[alt="Relative Image"]') expect(link['href']).to end_with('master/doc/README.md') - expect(image['src']).to end_with('master/app/assets/images/touch-icon-ipad.png') + expect(image['data-src']).to end_with('master/app/assets/images/touch-icon-ipad.png') end end @@ -70,7 +70,7 @@ module MarkdownMatchers # GollumTagsFilter matcher :parse_gollum_tags do def have_image(src) - have_css("img[src$='#{src}']") + have_css("img[data-src$='#{src}']") end prefix = '/namespace1/gitlabhq/wikis' |