Skip to content

Commit

Permalink
Add tests to the CRUD layout
Browse files Browse the repository at this point in the history
  • Loading branch information
malparty committed Dec 19, 2023
1 parent 5e8b4d2 commit 9e57e90
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
doctype html
html lang= I18n.locale
html lang=I18n.locale
head
meta charset="utf-8"
meta name="viewport" content="width=device-width, initial-scale=1.0"
title= content_for?(:title) ? yield(:title) : '<%= app_name %>'
= csrf_meta_tags
= csp_meta_tag
= stylesheet_link_tag 'application', :media => 'all'
= javascript_include_tag "application"
= javascript_include_tag 'application'

body class=class_names(controller_name.dasherize, action_name.dasherize)
header
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

describe 'CRUD Addon - application layout' do
it 'attaches the current locale to the html tag' do
expect(file('app/views/layouts/application.html.slim')).to contain('html lang=I18n.locale')
end

it 'loads the stylesheet entry file in the layout' do
expect(file('app/views/layouts/application.html.slim')).to contain("javascript_include_tag 'application'")
end

it 'loads the javascript entry file in the layout' do
expect(file('app/views/layouts/application.html.slim')).to contain("stylesheet_link_tag 'application'")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

describe 'CRUD Addon - application.scss' do
subject { file('app/assets/stylesheets/application.scss') }

it 'imports layouts stylesheets' do
expect(subject).to contain("@import './layouts';")
end
end
19 changes: 19 additions & 0 deletions .template/spec/addons/variants/web/crud/template_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

describe 'CRUD addon - template' do
it 'creates app/assets/stylesheets/layouts/index.scss' do
expect(file('app/assets/stylesheets/layouts/index.scss')).to exist
end

it 'removes app/views/layouts/application.html.erb' do
expect(file('app/views/layouts/application.html.erb')).not_to exist
end

it 'creates javascript/vendor/index.js' do
expect(file('app/javascript/vendor/index.js')).to exist
end

it 'creates app/views/layouts/application.html.slim' do
expect(file('app/views/layouts/application.html.slim')).to exist
end
end
8 changes: 0 additions & 8 deletions .template/spec/variants/web/app/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
expect(file('app/javascript/application.js')).to exist
end

it 'loads the javascript entry file in the layout' do
expect(file('app/views/layouts/application.html.erb')).to contain('<%= javascript_include_tag "application"')
end

it 'imports necessary modules' do
expect(file('app/javascript/application.js')).to contain('import \'./translations/translations\';')

Expand Down Expand Up @@ -103,9 +99,5 @@
it 'includes the localization concern in the application controller' do
expect(file('app/controllers/application_controller.rb')).to contain('include Localization')
end

it 'modifies the html tag to attach the current locale' do
expect(file('app/views/layouts/application.html.erb')).to contain("<html lang='<%= I18n.locale %>'>")
end
end
end
18 changes: 13 additions & 5 deletions .template/variants/web/app/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
# Javascript
directory 'app/javascript'

if File.exist?('app/views/layouts/application.html.erb')
insert_into_file 'app/views/layouts/application.html.erb', before: %r{</head>} do
erb_layout_file = 'app/views/layouts/application.html.erb'
slim_layout_file = 'app/views/layouts/application.html.slim'
layout_file = nil

layout_file = erb_layout_file if File.exist?(erb_layout_file)
layout_file = slim_layout_file if File.exist?(slim_layout_file)

if layout_file
insert_into_file layout_file, before: %r{</head>} do
<<~ERB.indent(2)
<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
ERB
end
else
@template_errors.add <<~ERROR
Cannot include javascript into `app/views/layouts/application.html.erb`
Cannot include javascript into `app/views/layouts/application.html.{erb|slim}`
Content: <%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
ERROR
end
Expand All @@ -33,11 +40,12 @@
RUBY
end

if File.exist?('app/views/layouts/application.html.erb')
if erb_layout_file
gsub_file 'app/views/layouts/application.html.erb', /<html>/ do
"<html lang='<%= I18n.locale %>'>"
end
else
# The slim layout (CRUD addon) already has the lang attribute
elsif slim_layout_file.blank?
@template_errors.add <<~ERROR
Cannot insert the lang attribute into html tag into `app/views/layouts/application.html.erb`
Content: <html lang='<%= I18n.locale %>'>
Expand Down

0 comments on commit 9e57e90

Please sign in to comment.