theme_helper_spec.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ThemeHelper do
  4. describe 'theme_style_tags' do
  5. let(:result) { helper.theme_style_tags(theme) }
  6. context 'when using system theme' do
  7. let(:theme) { 'system' }
  8. it 'returns the mastodon-light and default stylesheets with correct color schemes' do
  9. expect(html_links.first.attributes.symbolize_keys)
  10. .to include(
  11. href: have_attributes(value: match(/mastodon-light/)),
  12. media: have_attributes(value: 'not all and (prefers-color-scheme: dark)')
  13. )
  14. expect(html_links.last.attributes.symbolize_keys)
  15. .to include(
  16. href: have_attributes(value: match(/default/)),
  17. media: have_attributes(value: '(prefers-color-scheme: dark)')
  18. )
  19. end
  20. end
  21. context 'when using other theme' do
  22. let(:theme) { 'contrast' }
  23. it 'returns the theme stylesheet without color scheme information' do
  24. expect(html_links.first.attributes.symbolize_keys)
  25. .to include(
  26. href: have_attributes(value: match(/contrast/)),
  27. media: have_attributes(value: 'all')
  28. )
  29. end
  30. end
  31. end
  32. describe 'theme_color_tags' do
  33. let(:result) { helper.theme_color_tags(theme) }
  34. context 'when using system theme' do
  35. let(:theme) { 'system' }
  36. it 'returns the mastodon-light and default stylesheets with correct color schemes' do
  37. expect(html_theme_colors.first.attributes.symbolize_keys)
  38. .to include(
  39. content: have_attributes(value: Themes::THEME_COLORS[:dark]),
  40. media: have_attributes(value: '(prefers-color-scheme: dark)')
  41. )
  42. expect(html_theme_colors.last.attributes.symbolize_keys)
  43. .to include(
  44. content: have_attributes(value: Themes::THEME_COLORS[:light]),
  45. media: have_attributes(value: '(prefers-color-scheme: light)')
  46. )
  47. end
  48. end
  49. context 'when using mastodon-light theme' do
  50. let(:theme) { 'mastodon-light' }
  51. it 'returns the theme stylesheet without color scheme information' do
  52. expect(html_theme_colors.first.attributes.symbolize_keys)
  53. .to include(
  54. content: have_attributes(value: Themes::THEME_COLORS[:light])
  55. )
  56. end
  57. end
  58. context 'when using other theme' do
  59. let(:theme) { 'contrast' }
  60. it 'returns the theme stylesheet without color scheme information' do
  61. expect(html_theme_colors.first.attributes.symbolize_keys)
  62. .to include(
  63. content: have_attributes(value: Themes::THEME_COLORS[:dark])
  64. )
  65. end
  66. end
  67. end
  68. private
  69. def html_links
  70. Nokogiri::HTML5.fragment(result).css('link')
  71. end
  72. def html_theme_colors
  73. Nokogiri::HTML5.fragment(result).css('meta[name=theme-color]')
  74. end
  75. end