2019-07-25 05:27:42 +00:00
# frozen_string_literal: true
2016-11-19 21:59:32 +00:00
require 'action_dispatch/testing/test_request'
2016-08-29 16:18:59 +00:00
require 'fileutils'
module JavaScriptFixturesHelpers
2019-02-26 17:11:26 +00:00
extend ActiveSupport :: Concern
2016-08-29 16:18:59 +00:00
include Gitlab :: Popen
2019-03-12 17:20:46 +00:00
extend self
2016-08-29 16:18:59 +00:00
2019-02-26 17:11:26 +00:00
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
2021-03-04 15:11:19 +00:00
# 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 }
2019-04-21 10:00:13 +00:00
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
2019-03-12 17:20:46 +00:00
def fixture_root_path
2019-07-17 22:47:33 +00:00
'tmp/tests/frontend/fixtures' + ( Gitlab . ee? ? '-ee' : '' )
2019-03-12 17:20:46 +00:00
end
2016-08-29 16:18:59 +00:00
# Public: Removes all fixture files from given directory
#
2019-03-12 17:20:46 +00:00
# directory_name - directory of the fixtures (relative to .fixture_root_path)
2016-08-29 16:18:59 +00:00
#
def clean_frontend_fixtures ( directory_name )
2019-03-12 17:20:46 +00:00
full_directory_name = File . expand_path ( directory_name , fixture_root_path )
2021-03-17 21:11:29 +00:00
Dir [ File . expand_path ( '*.{html,json,md}' , full_directory_name ) ] . each do | file_name |
2019-03-12 17:20:46 +00:00
FileUtils . rm ( file_name )
2016-08-29 16:18:59 +00:00
end
end
2019-04-21 10:00:13 +00:00
def remove_repository ( project )
Gitlab :: Shell . new . remove_repository ( project . repository_storage , project . disk_path )
end
2020-10-15 09:08:41 +00:00
# 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
2019-04-21 10:00:13 +00:00
private
# Private: Store a response object as fixture file
2016-08-29 16:18:59 +00:00
#
2016-12-30 20:46:41 +00:00
# response - string or response object to store
2019-03-12 17:20:46 +00:00
# fixture_file_name - file name to store the fixture in (relative to .fixture_root_path)
2016-08-29 16:18:59 +00:00
#
def store_frontend_fixture ( response , fixture_file_name )
2019-03-12 17:20:46 +00:00
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
2019-03-12 17:20:46 +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 )
2016-08-29 16:18:59 +00:00
fixture = response . body
2017-01-03 21:26:14 +00:00
fixture . force_encoding ( " utf-8 " )
2016-08-29 16:18:59 +00:00
2020-01-29 18:08:47 +00:00
response_mime_type = Mime :: Type . lookup ( response . media_type )
2016-08-29 16:18:59 +00:00
if response_mime_type . html?
doc = Nokogiri :: HTML :: DocumentFragment . parse ( fixture )
2016-11-23 20:36:34 +00:00
link_tags = doc . css ( 'link' )
link_tags . remove
2019-09-27 15:06:16 +00:00
scripts = doc . css ( " script:not([type='text/template']):not([type='text/x-template']):not([type='application/json']) " )
2016-08-29 16:18:59 +00:00
scripts . remove
fixture = doc . to_html
# replace relative links
2016-11-19 21:59:32 +00:00
test_host = ActionDispatch :: TestRequest :: DEFAULT_ENV [ 'HTTP_HOST' ]
fixture . gsub! ( %r{ ="/ } , " = \" http:// #{ test_host } / " )
2016-08-29 16:18:59 +00:00
end
2016-12-30 20:46:41 +00:00
fixture
2016-08-29 16:18:59 +00:00
end
end