app.kluk.fr/spec/requests/day_logs_spec.rb
Christophe Robillard a9f72b60af can edit selected day
2026-03-21 16:16:54 +01:00

251 lines
8 KiB
Ruby

require 'rails_helper'
describe "DayLogs", type: :request do
let(:user) { create(:user) }
let(:mode) { create(:mode, user: user) }
let(:day) { Date.parse("2026-01-15") }
describe "GET /day_logs/edit" do
context "when not authenticated" do
it "redirects to the login page" do
get edit_day_log_for_day_path(day: day)
expect(response).to redirect_to(new_session_path)
end
end
context "when authenticated" do
before { login_as(user) }
context "with an existing day_log for the day" do
let!(:day_log) { create(:day_log, user: user, day: day) }
it "returns http success" do
get edit_day_log_for_day_path(day: day)
expect(response).to have_http_status(:success)
end
end
context "without an existing day_log for the day" do
it "returns http success" do
get edit_day_log_for_day_path(day: day)
expect(response).to have_http_status(:success)
end
it "does not persist a day_log" do
expect {
get edit_day_log_for_day_path(day: day)
}.not_to change(DayLog, :count)
end
end
end
end
describe "POST /day_logs" do
context "when not authenticated" do
it "redirects to the login page" do
post day_logs_path, params: { day_log: { day: day, info: "Une info" } }
expect(response).to redirect_to(new_session_path)
end
end
context "when authenticated" do
before { login_as(user) }
context "with valid params" do
context "without mode" do
it "creates a day_log" do
expect {
post day_logs_path, params: { day_log: { day: day, info: "Une info" } }
}.to change(DayLog, :count).by(1)
end
it "does not create a mood" do
expect {
post day_logs_path, params: { day_log: { day: day, info: "Une info" } }
}.not_to change(Mood, :count)
end
end
context "with mode, no existing mood" do
it "creates a day_log" do
expect {
post day_logs_path, params: { day_log: { day: day, info: "Une info", mode_id: mode.id } }
}.to change(DayLog, :count).by(1)
end
it "creates a mood" do
expect {
post day_logs_path, params: { day_log: { day: day, info: "Une info", mode_id: mode.id } }
}.to change(Mood, :count).by(1)
end
end
context "with mode, existing mood" do
let!(:existing_mood) { create(:mood, user: user, mode: mode, recorded_at: DateTime.parse("2026-01-15 08:00:00")) }
let(:other_mode) { create(:mode, user: user) }
it "creates a day_log" do
expect {
post day_logs_path, params: { day_log: { day: day, info: "Une info", mode_id: other_mode.id } }
}.to change(DayLog, :count).by(1)
end
it "does not create a new mood" do
expect {
post day_logs_path, params: { day_log: { day: day, info: "Une info", mode_id: other_mode.id } }
}.not_to change(Mood, :count)
end
it "updates the existing mood mode" do
post day_logs_path, params: { day_log: { day: day, info: "Une info", mode_id: other_mode.id } }
expect(existing_mood.reload.mode).to eq(other_mode)
end
end
it "redirects after creation" do
post day_logs_path, params: { day_log: { day: day, info: "Une info" } }
expect(response).to have_http_status(:redirect)
end
end
context "with invalid params" do
it "does not create a day_log" do
expect {
post day_logs_path, params: { day_log: { day: nil, info: nil } }
}.not_to change(DayLog, :count)
end
it "returns unprocessable content" do
post day_logs_path, params: { day_log: { day: nil, info: nil } }
expect(response).to have_http_status(:unprocessable_content)
end
end
end
end
describe "GET /day_logs/:id/edit" do
let!(:day_log) { create(:day_log, user: user, day: day) }
context "when not authenticated" do
it "redirects to the login page" do
get edit_day_log_path(day_log)
expect(response).to redirect_to(new_session_path)
end
end
context "when authenticated" do
before { login_as(user) }
it "returns http success" do
get edit_day_log_path(day_log)
expect(response).to have_http_status(:success)
end
end
end
describe "GET /day_logs/edit" do
context "when not authenticated" do
it "redirects to the login page" do
get edit_day_log_for_day_path(day: day)
expect(response).to redirect_to(new_session_path)
end
end
context "when authenticated" do
before { login_as(user) }
context "with an existing day_log for the day" do
let!(:day_log) { create(:day_log, user: user, day: day) }
it "returns http success" do
get edit_day_log_for_day_path(day: day)
expect(response).to have_http_status(:success)
end
end
context "without an existing day_log for the day" do
it "returns http success" do
get edit_day_log_for_day_path(day: day)
expect(response).to have_http_status(:success)
end
it "does not persist a day_log" do
expect {
get edit_day_log_for_day_path(day: day)
}.not_to change(DayLog, :count)
end
end
end
end
describe "PATCH /day_logs/:id" do
let!(:day_log) { create(:day_log, user: user, day: day) }
context "when not authenticated" do
it "redirects to the login page" do
patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info" } }
expect(response).to redirect_to(new_session_path)
end
end
context "when authenticated" do
before { login_as(user) }
context "with valid params" do
context "without mode" do
it "updates the day_log" do
patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info" } }
expect(day_log.reload.info).to eq("Nouvelle info")
end
it "does not create a mood" do
expect {
patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info" } }
}.not_to change(Mood, :count)
end
it "redirects to dashboard" do
patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info" } }
expect(response).to redirect_to(dashboard_path)
end
end
context "with mode, no existing mood" do
it "creates a mood" do
expect {
patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info", mode_id: mode.id } }
}.to change(Mood, :count).by(1)
end
end
context "with mode, existing mood" do
let!(:existing_mood) { create(:mood, user: user, mode: mode, recorded_at: DateTime.parse("2026-01-15 08:00:00")) }
let(:other_mode) { create(:mode, user: user) }
it "does not create a new mood" do
expect {
patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info", mode_id: other_mode.id } }
}.not_to change(Mood, :count)
end
it "updates the existing mood mode" do
patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info", mode_id: other_mode.id } }
expect(existing_mood.reload.mode).to eq(other_mode)
end
end
end
context "with invalid params" do
it "does not update the day_log" do
patch day_log_path(day_log), params: { day_log: { info: nil } }
expect(day_log.reload.info).not_to be_nil
end
it "returns unprocessable content" do
patch day_log_path(day_log), params: { day_log: { info: nil } }
expect(response).to have_http_status(:unprocessable_content)
end
end
end
end
end