|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +module Users |
| 4 | + class HeatmapsControllerTest < ActionDispatch::IntegrationTest |
| 5 | + ## |
| 6 | + # test all routes which lead to this controller |
| 7 | + def test_routes |
| 8 | + assert_routing( |
| 9 | + { :path => "/user/username/heatmap", :method => :get }, |
| 10 | + { :controller => "users/heatmaps", :action => "show", :user_display_name => "username" } |
| 11 | + ) |
| 12 | + end |
| 13 | + |
| 14 | + def test_show_data |
| 15 | + user = create(:user) |
| 16 | + # Create two changesets |
| 17 | + create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 10) |
| 18 | + create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20) |
| 19 | + |
| 20 | + get user_heatmap_path(user) |
| 21 | + |
| 22 | + assert_response :success |
| 23 | + # The data should not be empty |
| 24 | + heatmap_data = assigns(:heatmap_data) |
| 25 | + assert_not_nil heatmap_data |
| 26 | + assert_predicate heatmap_data[:data], :any? |
| 27 | + # The data should be in the right format |
| 28 | + heatmap_data[:data].each_value do |entry| |
| 29 | + assert_equal [:date, :max_id, :total_changes], entry.keys.sort, "Heatmap data entries should have expected keys" |
| 30 | + end |
| 31 | + assert_equal 30, heatmap_data[:count] |
| 32 | + end |
| 33 | + |
| 34 | + def test_show_data_caching |
| 35 | + # Enable caching to be able to test |
| 36 | + Rails.cache.clear |
| 37 | + @original_cache_store = Rails.cache |
| 38 | + Rails.cache = ActiveSupport::Cache::MemoryStore.new |
| 39 | + |
| 40 | + user = create(:user) |
| 41 | + |
| 42 | + # Create an initial changeset |
| 43 | + create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 15) |
| 44 | + |
| 45 | + # First request to populate the cache |
| 46 | + get user_heatmap_path(user) |
| 47 | + first_response_data = assigns(:heatmap_data) |
| 48 | + assert_not_nil first_response_data, "Expected heatmap data to be assigned on the first request" |
| 49 | + assert_equal 1, first_response_data[:data].values.count { |day| day[:total_changes].positive? }, "Expected one entry in the heatmap data" |
| 50 | + |
| 51 | + # Inspect cache after the first request |
| 52 | + cached_data = Rails.cache.read("heatmap_data_of_user_#{user.id}") |
| 53 | + assert_equal first_response_data, cached_data, "Expected the cache to contain the first response data" |
| 54 | + |
| 55 | + # Add a new changeset to the database |
| 56 | + create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20) |
| 57 | + |
| 58 | + # Second request |
| 59 | + get user_heatmap_path(user) |
| 60 | + second_response_data = assigns(:heatmap_data) |
| 61 | + |
| 62 | + # Confirm that the cache is still being used |
| 63 | + assert_equal first_response_data, second_response_data, "Expected cached data to be returned on the second request" |
| 64 | + |
| 65 | + # Clear the cache and make a third request to confirm new data is retrieved |
| 66 | + Rails.cache.clear |
| 67 | + get user_heatmap_path(user) |
| 68 | + third_response_data = assigns(:heatmap_data) |
| 69 | + |
| 70 | + # Ensure the new entry is now included |
| 71 | + assert_equal 2, third_response_data[:data].values.count { |day| day[:total_changes].positive? }, "Expected two entries in the heatmap data after clearing the cache" |
| 72 | + |
| 73 | + # Reset caching config to defaults |
| 74 | + Rails.cache.clear |
| 75 | + Rails.cache = @original_cache_store |
| 76 | + end |
| 77 | + |
| 78 | + def test_show_data_no_changesets |
| 79 | + user = create(:user) |
| 80 | + |
| 81 | + get user_heatmap_path(user) |
| 82 | + |
| 83 | + assert_response :success |
| 84 | + assert_empty(assigns(:heatmap_data)[:data].values) |
| 85 | + assert_select ".heatmap", :count => 0 |
| 86 | + end |
| 87 | + |
| 88 | + def test_show_rendering_of_user_with_no_changesets |
| 89 | + user_without_changesets = create(:user) |
| 90 | + |
| 91 | + get user_heatmap_path(user_without_changesets) |
| 92 | + |
| 93 | + assert_response :success |
| 94 | + assert_select ".heatmap", 0 |
| 95 | + end |
| 96 | + |
| 97 | + def test_show_rendering_of_user_with_changesets |
| 98 | + user = create(:user) |
| 99 | + changeset39 = create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 39) |
| 100 | + _changeset5 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 5) |
| 101 | + changeset11 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 11) |
| 102 | + |
| 103 | + get user_heatmap_path(user) |
| 104 | + |
| 105 | + assert_response :success |
| 106 | + assert_select ".heatmap a", 2 |
| 107 | + |
| 108 | + history_path = user_history_path(user) |
| 109 | + assert_select ".heatmap a[data-date='#{4.months.ago.to_date}'][data-count='39'][href='#{history_path}?before=#{changeset39.id + 1}']" |
| 110 | + assert_select ".heatmap a[data-date='#{3.months.ago.to_date}'][data-count='16'][href='#{history_path}?before=#{changeset11.id + 1}']" |
| 111 | + assert_select ".heatmap [data-date='#{5.months.ago.to_date}']:not([data-count])" |
| 112 | + end |
| 113 | + |
| 114 | + def test_headline_changeset_zero |
| 115 | + user = create(:user) |
| 116 | + |
| 117 | + get user_heatmap_path(user) |
| 118 | + |
| 119 | + assert_response :success |
| 120 | + assert_select "h2.text-body-secondary.fs-5", :count => 0 |
| 121 | + end |
| 122 | + |
| 123 | + def test_headline_changeset_singular |
| 124 | + user = create(:user) |
| 125 | + create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 1) |
| 126 | + |
| 127 | + get user_heatmap_path(user) |
| 128 | + |
| 129 | + assert_response :success |
| 130 | + assert_select "h2.text-body-secondary.fs-5", :text => "1 contribution in the last year" |
| 131 | + end |
| 132 | + |
| 133 | + def test_headline_changeset_plural |
| 134 | + user = create(:user) |
| 135 | + create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 12) |
| 136 | + |
| 137 | + get user_heatmap_path(user) |
| 138 | + |
| 139 | + assert_response :success |
| 140 | + assert_select "h2.text-body-secondary.fs-5", :text => "12 contributions in the last year" |
| 141 | + end |
| 142 | + end |
| 143 | +end |
0 commit comments