test_css_files_building.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import pytest
  2. from ..css import change_css_source, get_theme_media_map
  3. @pytest.fixture
  4. def assert_snapshot_match(snapshot, theme):
  5. def _assert_snapshot_match(result):
  6. result = result.replace(theme.dirname, "themedir")
  7. snapshot.assert_match(result)
  8. return _assert_snapshot_match
  9. @pytest.fixture
  10. def media_map(theme, image):
  11. return get_theme_media_map(theme)
  12. def test_simple_url_to_file_is_replaced_with_valid_url(
  13. assert_snapshot_match, media_map, image
  14. ):
  15. css = ".page-header { background-image: url(%s); }" % image.name
  16. result = change_css_source(media_map, css)
  17. assert_snapshot_match(result)
  18. def test_relative_url_to_file_is_replaced_with_valid_url(
  19. assert_snapshot_match, media_map, image
  20. ):
  21. css = ".page-header { background-image: url(./%s); }" % image.name
  22. result = change_css_source(media_map, css)
  23. assert_snapshot_match(result)
  24. def test_url_to_file_from_create_react_app_is_replaced_with_valid_url(
  25. assert_snapshot_match, media_map, image
  26. ):
  27. hashed_name = str(image.file).split("/")[-1]
  28. css = ".page-header { background-image: url(/static/media/%s); }" % hashed_name
  29. result = change_css_source(media_map, css)
  30. assert_snapshot_match(result)
  31. def test_quoted_url_to_file_is_replaced_with_valid_url(
  32. assert_snapshot_match, media_map, image
  33. ):
  34. css = '.page-header { background-image: url("%s"); }' % image.name
  35. result = change_css_source(media_map, css)
  36. assert_snapshot_match(result)
  37. def test_single_quoted_url_to_file_is_replaced_with_valid_url(
  38. assert_snapshot_match, media_map, image
  39. ):
  40. css = ".page-header { background-image: url('%s'); }" % image.name
  41. result = change_css_source(media_map, css)
  42. assert_snapshot_match(result)
  43. def test_absolute_https_url_to_file_is_not_replaced(media_map):
  44. css = ".page-header { background-image: url(https://cdn.example.com/bg.png); }"
  45. result = change_css_source(media_map, css)
  46. assert result == css
  47. def test_absolute_http_url_to_file_is_not_replaced(media_map):
  48. css = ".page-header { background-image: url(http://cdn.example.com/bg.png); }"
  49. result = change_css_source(media_map, css)
  50. assert result == css
  51. def test_absolute_protocol_relative_url_to_file_is_not_replaced(media_map):
  52. css = ".page-header { background-image: url(://cdn.example.com/bg.png); }"
  53. result = change_css_source(media_map, css)
  54. assert result == css
  55. def test_css_file_with_multiple_different_urls_is_correctly_replaced(
  56. assert_snapshot_match, media_map, image
  57. ):
  58. css = (
  59. ".page-header { background-image: url(http://cdn.example.com/bg.png); }"
  60. '\n.container { background-image: url("%s"); }'
  61. '\n.alert { background-image: url("%s"); }'
  62. ) % (image.name, str(image.file).strip("/")[-1])
  63. result = change_css_source(media_map, css)
  64. assert_snapshot_match(result)