gitlab-org--gitlab-foss/spec/support/helpers/javascript_fixtures_helpers.rb

97 lines
3.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'action_dispatch/testing/test_request'
require 'fileutils'
module JavaScriptFixturesHelpers
extend ActiveSupport::Concern
include Gitlab::Popen
extend self
included do |base|
2018-10-29 21:36:28 +00:00
base.around do |example|
# pick an arbitrary date from the past, so tests are not time dependent
# Also see spec/frontend/__helpers__/fake_date/jest.js
2018-10-29 21:36:28 +00:00
Timecop.freeze(Time.utc(2015, 7, 3, 10)) { example.run }
raise NoMethodError.new('You need to set `response` for the fixture generator! This will automatically happen with `type: :controller` or `type: :request`.', 'response') unless respond_to?(:response)
store_frontend_fixture(response, example.description)
2018-10-29 21:36:28 +00:00
end
end
def fixture_root_path
2019-07-17 22:47:33 +00:00
'tmp/tests/frontend/fixtures' + (Gitlab.ee? ? '-ee' : '')
end
# Public: Removes all fixture files from given directory
#
# directory_name - directory of the fixtures (relative to .fixture_root_path)
#
def clean_frontend_fixtures(directory_name)
full_directory_name = File.expand_path(directory_name, fixture_root_path)
Dir[File.expand_path('*.{html,json,md}', full_directory_name)].each do |file_name|
FileUtils.rm(file_name)
end
end
def remove_repository(project)
Gitlab::Shell.new.remove_repository(project.repository_storage, project.disk_path)
end
# Public: Reads a GraphQL query from the filesystem as a string
#
# query_path - file path to the GraphQL query, relative to `app/assets/javascripts`
# fragment_paths - an optional array of file paths to any fragments the query uses,
# also relative to `app/assets/javascripts`
def get_graphql_query_as_string(query_path, fragment_paths = [])
[query_path, *fragment_paths].map do |path|
File.read(File.join(Rails.root, '/app/assets/javascripts', path))
end.join("\n")
end
private
# Private: Store a response object as fixture file
#
2016-12-30 20:46:41 +00:00
# response - string or response object to store
# fixture_file_name - file name to store the fixture in (relative to .fixture_root_path)
#
def store_frontend_fixture(response, fixture_file_name)
full_fixture_path = File.expand_path(fixture_file_name, fixture_root_path)
fixture = response.respond_to?(:body) ? parse_response(response) : response
2016-12-30 20:46:41 +00:00
FileUtils.mkdir_p(File.dirname(full_fixture_path))
File.write(full_fixture_path, fixture)
2016-12-30 20:46:41 +00:00
end
# Private: Prepare a response object for use as a frontend fixture
#
# response - response object to prepare
#
def parse_response(response)
fixture = response.body
fixture.force_encoding("utf-8")
response_mime_type = Mime::Type.lookup(response.media_type)
if response_mime_type.html?
doc = Nokogiri::HTML::DocumentFragment.parse(fixture)
link_tags = doc.css('link')
link_tags.remove
scripts = doc.css("script:not([type='text/template']):not([type='text/x-template']):not([type='application/json'])")
scripts.remove
fixture = doc.to_html
# replace relative links
test_host = ActionDispatch::TestRequest::DEFAULT_ENV['HTTP_HOST']
fixture.gsub!(%r{="/}, "=\"http://#{test_host}/")
end
2016-12-30 20:46:41 +00:00
fixture
end
end