Skip to content

Commit c2c08ba

Browse files
etewiahclaude
andcommitted
Fix memoization issues in styleable and setup specs
Use fresh database instances instead of reloaded objects in tests where memoized @current_theme needs to be cleared. Calling reload on a model doesn't clear memoized instance variables. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5e4fbf8 commit c2c08ba

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

spec/models/concerns/pwb/website_styleable_spec.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@
139139

140140
it "returns nil for invalid theme_name" do
141141
website.update_column(:theme_name, "nonexistent")
142+
# Get a fresh instance since reload doesn't clear memoized @current_theme
143+
fresh_website = Pwb::Website.find(website.id)
142144

143-
expect(website.current_theme).to be_nil
145+
expect(fresh_website.current_theme).to be_nil
144146
end
145147

146148
it "memoizes the result" do
@@ -190,12 +192,12 @@
190192
end
191193

192194
context "with no current theme" do
193-
before do
195+
it "returns nil" do
194196
website.update_column(:theme_name, "nonexistent")
195-
end
197+
# Get a fresh instance since reload doesn't clear memoized @current_theme
198+
fresh_website = Pwb::Website.find(website.id)
196199

197-
it "returns nil" do
198-
expect(website.effective_palette_id).to be_nil
200+
expect(fresh_website.effective_palette_id).to be_nil
199201
end
200202
end
201203
end
@@ -234,8 +236,10 @@
234236

235237
it "returns false when no theme" do
236238
website.update_column(:theme_name, "nonexistent")
239+
# Get a fresh instance since reload doesn't clear memoized @current_theme
240+
fresh_website = Pwb::Website.find(website.id)
237241

238-
result = website.apply_palette!("ocean_blue")
242+
result = fresh_website.apply_palette!("ocean_blue")
239243

240244
expect(result).to be false
241245
end
@@ -254,8 +258,10 @@
254258

255259
it "returns empty hash when no theme" do
256260
website.update_column(:theme_name, "nonexistent")
261+
# Get a fresh instance since reload doesn't clear memoized @current_theme
262+
fresh_website = Pwb::Website.find(website.id)
257263

258-
expect(website.available_palettes).to eq({})
264+
expect(fresh_website.available_palettes).to eq({})
259265
end
260266

261267
it "returns theme-specific palettes" do
@@ -289,8 +295,10 @@
289295

290296
it "returns empty array when no theme" do
291297
website.update_column(:theme_name, "nonexistent")
298+
# Get a fresh instance since reload doesn't clear memoized @current_theme
299+
fresh_website = Pwb::Website.find(website.id)
292300

293-
expect(website.palette_options_for_select).to eq([])
301+
expect(fresh_website.palette_options_for_select).to eq([])
294302
end
295303
end
296304

spec/models/pwb/website_spec.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,11 @@ module Pwb
238238
expect(website.selected_palette).to eq('ocean_blue')
239239
end
240240

241-
it 'defaults to nil' do
241+
it 'defaults to theme default palette when theme has palettes' do
242+
# Website factory sets theme_name to 'default', which triggers ensure_palette_selected
243+
# to auto-set the theme's default palette
242244
new_website = FactoryBot.create(:pwb_website)
243-
expect(new_website.selected_palette).to be_nil
245+
expect(new_website.selected_palette).to eq('classic_red')
244246
end
245247
end
246248

@@ -255,8 +257,11 @@ module Pwb
255257

256258
it 'returns nil for invalid theme' do
257259
website.update_column(:theme_name, 'nonexistent')
260+
# Get a fresh instance since update_column bypasses callbacks and
261+
# reload doesn't clear memoized @current_theme
262+
fresh_website = Pwb::Website.find(website.id)
258263

259-
expect(website.current_theme).to be_nil
264+
expect(fresh_website.current_theme).to be_nil
260265
end
261266
end
262267

@@ -337,8 +342,11 @@ module Pwb
337342

338343
it 'returns empty hash when no theme' do
339344
website.update_column(:theme_name, 'nonexistent')
345+
# Get a fresh instance since update_column bypasses callbacks and
346+
# reload doesn't clear memoized @current_theme
347+
fresh_website = Pwb::Website.find(website.id)
340348

341-
expect(website.available_palettes).to eq({})
349+
expect(fresh_website.available_palettes).to eq({})
342350
end
343351
end
344352

spec/requests/pwb/setup_spec.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@
66
# Setup controller handles initial website creation when no website exists
77
# for the current subdomain. This is the onboarding flow for new tenants.
88

9+
# Ensure TenantSettings allows all themes for tests
10+
before(:all) do
11+
Pwb::TenantSettings.delete_all
12+
Pwb::TenantSettings.create!(
13+
singleton_key: 'default',
14+
default_available_themes: %w[default brisbane bologna barcelona biarritz]
15+
)
16+
end
17+
18+
after(:all) do
19+
Pwb::TenantSettings.delete_all
20+
end
21+
922
describe 'GET /setup (index)' do
1023
context 'when no website exists for subdomain' do
1124
it 'renders the setup page successfully' do
@@ -84,8 +97,8 @@
8497
website = Pwb::Website.find_by(subdomain: 'themedsite')
8598
# Website should be created with some theme (default or from pack)
8699
expect(website).to be_present
87-
# Theme defaults to 'default' if not specified in pack
88-
expect(website.theme_name).to eq('default').or eq('brisbane').or be_nil
100+
# netherlands_urban pack uses 'bologna' theme
101+
expect(website.theme_name).to eq('bologna')
89102
end
90103
end
91104

0 commit comments

Comments
 (0)