From a0aede40642fa84aa05ba2f602a237622a06e8f9 Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Fri, 21 Jun 2024 11:06:22 +0200 Subject: [PATCH] Add hook to access cutoff state REDMINE-20674 --- .../entry_json_seed/_entry.json.jbuilder | 2 + .../package/spec/entryState/cutoff-spec.js | 17 ++++++ .../scrolled/package/src/entryState/cutoff.js | 6 ++ .../scrolled/package/src/entryState/index.js | 1 + .../scrolled/package/src/frontend/index.js | 1 + .../package/src/testHelpers/normalizeSeed.js | 2 + .../entry_json_seed_helper_spec.rb | 61 +++++++++++++++++++ 7 files changed, 90 insertions(+) create mode 100644 entry_types/scrolled/package/spec/entryState/cutoff-spec.js create mode 100644 entry_types/scrolled/package/src/entryState/cutoff.js diff --git a/entry_types/scrolled/app/views/pageflow_scrolled/entry_json_seed/_entry.json.jbuilder b/entry_types/scrolled/app/views/pageflow_scrolled/entry_json_seed/_entry.json.jbuilder index 663dee7f1e..4146e08c4b 100644 --- a/entry_types/scrolled/app/views/pageflow_scrolled/entry_json_seed/_entry.json.jbuilder +++ b/entry_types/scrolled/app/views/pageflow_scrolled/entry_json_seed/_entry.json.jbuilder @@ -46,6 +46,8 @@ json.config do json.file_licenses( I18n.t('pageflow.file_licenses', default: {}).slice(*entry_config.available_file_licenses) ) + + json.cut_off entry.cutoff_mode_enabled_for?(request) end unless options[:skip_i18n] diff --git a/entry_types/scrolled/package/spec/entryState/cutoff-spec.js b/entry_types/scrolled/package/spec/entryState/cutoff-spec.js new file mode 100644 index 0000000000..919c4462c7 --- /dev/null +++ b/entry_types/scrolled/package/spec/entryState/cutoff-spec.js @@ -0,0 +1,17 @@ +import {useCutOff} from 'entryState'; + +import {renderHookInEntry} from 'support'; + +describe('useCutOff', () => { + it('reads data from config', () => { + const {result} = renderHookInEntry( + () => useCutOff(), { + seed: { + cutOff: true + } + } + ); + + expect(result.current).toEqual(true); + }); +}); diff --git a/entry_types/scrolled/package/src/entryState/cutoff.js b/entry_types/scrolled/package/src/entryState/cutoff.js new file mode 100644 index 0000000000..5cc9200dcb --- /dev/null +++ b/entry_types/scrolled/package/src/entryState/cutoff.js @@ -0,0 +1,6 @@ +import {useEntryStateConfig} from "./EntryStateProvider"; + +export function useCutOff() { + const config = useEntryStateConfig(); + return config.cutOff; +} diff --git a/entry_types/scrolled/package/src/entryState/index.js b/entry_types/scrolled/package/src/entryState/index.js index 153a6bdc11..9a09a07b05 100644 --- a/entry_types/scrolled/package/src/entryState/index.js +++ b/entry_types/scrolled/package/src/entryState/index.js @@ -1,5 +1,6 @@ export {useAdditionalSeedData} from './additionalSeedData'; export {useContentElementConsentVendor} from './consentVendors'; +export {useCutOff} from './cutoff'; export {useShareProviders, useShareUrl} from './sharing'; export {useEntryTranslations} from './entryTranslations'; export {useEntryMetadata} from './metadata'; diff --git a/entry_types/scrolled/package/src/frontend/index.js b/entry_types/scrolled/package/src/frontend/index.js index f15c600911..6f6f87a388 100644 --- a/entry_types/scrolled/package/src/frontend/index.js +++ b/entry_types/scrolled/package/src/frontend/index.js @@ -63,6 +63,7 @@ export { useAdditionalSeedData, useChapters, useCredits, + useCutOff, useEntryMetadata, useEntryStateDispatch, useEntryTranslations, diff --git a/entry_types/scrolled/package/src/testHelpers/normalizeSeed.js b/entry_types/scrolled/package/src/testHelpers/normalizeSeed.js index 15171bb915..a4bbea58e4 100644 --- a/entry_types/scrolled/package/src/testHelpers/normalizeSeed.js +++ b/entry_types/scrolled/package/src/testHelpers/normalizeSeed.js @@ -46,6 +46,7 @@ export function normalizeSeed({ additionalSeedData, consentVendors, contentElementConsentVendors, + cutOff, fileLicenses, entryTranslations } = {}) { @@ -90,6 +91,7 @@ export function normalizeSeed({ }, consentVendors: consentVendors || [], contentElementConsentVendors: contentElementConsentVendors || {}, + cutOff, fileLicenses: fileLicenses || {}, entryTranslations: entryTranslations || [] }, diff --git a/entry_types/scrolled/spec/helpers/pageflow_scrolled/entry_json_seed_helper_spec.rb b/entry_types/scrolled/spec/helpers/pageflow_scrolled/entry_json_seed_helper_spec.rb index b3b2e8e0ce..867386fa18 100644 --- a/entry_types/scrolled/spec/helpers/pageflow_scrolled/entry_json_seed_helper_spec.rb +++ b/entry_types/scrolled/spec/helpers/pageflow_scrolled/entry_json_seed_helper_spec.rb @@ -1127,6 +1127,67 @@ def render(helper, entry, options = {}) }) end end + + context 'cutoff' do + it 'renders false by default' do + entry = create(:published_entry, + type_name: 'scrolled') + + result = render(helper, entry) + + expect(result).to include_json(config: {cutOff: false}) + end + + it 'renders cutoff mode result' do + pageflow_configure do |config| + config.cutoff_modes.register( + :test, + proc { true } + ) + end + + site = create(:site, cutoff_mode_name: 'test') + entry = create(:published_entry, + site:, + type_name: 'scrolled', + revision_attributes: { + configuration: { + cutoff_section_perma_id: 100 + } + }) + chapter = create(:scrolled_chapter, position: 2, revision: entry.revision) + create(:section, chapter:, position: 2, perma_id: 100) + + result = render(helper, entry) + + expect(result).to include_json(config: {cutOff: true}) + end + + it 'renders false for draft entry' do + pageflow_configure do |config| + config.cutoff_modes.register( + :test, + proc { true } + ) + end + + site = create(:site, cutoff_mode_name: 'test') + entry = create(:draft_entry, + site:, + type_name: 'scrolled', + revision_attributes: { + configuration: { + cutoff_section_perma_id: 100 + } + }) + chapter = create(:scrolled_chapter, position: 2, revision: entry.revision) + create(:section, chapter:, position: 2, perma_id: 100) + + result = render(helper, entry) + + expect(result).to include_json(config: {cutOff: false}) + end + end end describe '#scrolled_entry_json_seed_script_tag' do