From b2877c680f074acd2be0b32edd0a13ff1ed05729 Mon Sep 17 00:00:00 2001 From: Julia Nguyen Date: Mon, 29 Oct 2018 20:30:19 -0400 Subject: [PATCH] Shorten methods in MedicationsHelper and Input component, add Google Calendar reminder to print_reminders (#1219) * Shorten methods in MedicationsHelper * Shorten Input component * Readd medication_weekly_dosage * Add Google Calendar reminder to print_reminders --- app/helpers/medication_refill_helper.rb | 6 +- app/helpers/medications_helper.rb | 285 ++++++++++-------- app/helpers/reminder_helper.rb | 20 +- client/app/components/Form/index.jsx | 5 +- client/app/components/Input/InputCheckbox.jsx | 2 +- .../components/Input/InputCheckboxGroup.jsx | 2 +- client/app/components/Input/InputSelect.jsx | 2 +- client/app/components/Input/InputTag.jsx | 2 +- client/app/components/Input/index.jsx | 126 ++------ client/app/components/Input/utils.js | 89 ++++++ .../app/components/Tag/__tests__/Tag.spec.jsx | 8 +- client/app/mocks/InputMocks.jsx | 2 +- client/app/widgets/QuickCreate/index.jsx | 2 +- .../Resources/__tests__/Resources.spec.jsx | 4 +- .../user_creates_a_medication_spec.rb | 14 +- spec/helpers/medications_helper_spec.rb | 4 +- spec/helpers/reminder_helper_spec.rb | 43 ++- 17 files changed, 345 insertions(+), 271 deletions(-) create mode 100644 client/app/components/Input/utils.js diff --git a/app/helpers/medication_refill_helper.rb b/app/helpers/medication_refill_helper.rb index fdecd89dfd..9b23ddc309 100644 --- a/app/helpers/medication_refill_helper.rb +++ b/app/helpers/medication_refill_helper.rb @@ -15,10 +15,12 @@ def save_refill_to_google_calendar(medication) end def calendar_uploader_params(medication) - { summary: "Refill for #{medication.name}", + { + summary: "Refill for #{medication.name}", date: medication.refill, access_token: current_user.google_access_token, - email: current_user.email } + email: current_user.email + } end private diff --git a/app/helpers/medications_helper.rb b/app/helpers/medications_helper.rb index 585c380b89..ab84b4a80a 100644 --- a/app/helpers/medications_helper.rb +++ b/app/helpers/medications_helper.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true - # rubocop:disable ModuleLength module MedicationsHelper include FormHelper @@ -18,120 +17,151 @@ def medication_form_inputs current_user.google_oauth2_enabled? ? google_fields : common_fields end - # rubocop:disable MethodLength + def medication_basic_props(field) + { + id: "medication_#{field}", + name: "medication[#{field}]", + dark: true + } + end + + def refill_reminder(field, info, checked, label = nil) + { + id: "medication_#{field}", + type: 'checkbox', + label: label || t("medications.#{field}"), + info: info, + name: "medication[#{field}][active]", + checked: checked + }.merge(dark: true, uncheckedValue: false, value: true) + end + + def medication_refill_reminder + refill_reminder( + 'refill_reminder_attributes', + t('medications.form.refill_reminder_hint'), + @medication&.refill_reminder&.active, + t('medications.refill_reminder') + ) + end + + def medication_take_medication_reminder_attributes + refill_reminder( + 'take_medication_reminder_attributes', + t('medications.form.daily_reminder_hint'), + @medication&.take_medication_reminder&.active, + t('common.daily_reminder') + ) + end + + def hidden_fields + [ + hidden_props( + 'refill_reminder_attributes', + @medication&.refill_reminder&.id + ), + hidden_props( + 'take_medication_reminder_attributes', + @medication&.take_medication_reminder&.id + ) + ] + end + + def reminder_fields + [ + medication_refill_reminder, + medication_take_medication_reminder_attributes + ].concat(hidden_fields) + end + + def hidden_props(field, value) + { + id: "medication_#{field}_id", + name: "medication[#{field}][id]", + type: 'hidden', + value: value + } + end + + def medication_weekly_dosage + { + type: 'checkboxGroup', + checkboxes: days_checkbox, + label: t('common.days'), + info: t('medications.form.weekly_dosage_hint'), + required: true + }.merge(medication_basic_props('weekly_dosage')) + end + + def extra_fields + [medication_weekly_dosage, medication_refill, medication_comments] + end + def common_fields [ - { - id: 'medication_name', - type: 'text', - name: 'medication[name]', - label: t('common.name'), - value: @medication.name || nil, - required: true, - info: t('categories.form.name_hint'), - dark: true - }, + medication_name, medication_field('strength'), - { - id: 'medication_strength_unit', - type: 'select', - name: 'medication[strength_unit]', - value: @medication.strength_unit || t('medications.units.mg'), - dark: true, - options: [ - { - id: 'medication_strength_unit_mg', - label: t('medications.units.mg'), - value: t('medications.units.mg') - }, - { - id: 'medication_strength_unit_ml', - label: t('medications.units.ml'), - value: t('medications.units.ml') - } - ] - }, + medication_strength_unit, medication_field('total'), medication_unit_field('total'), medication_field('dosage'), - medication_unit_field('dosage'), - { - id: 'medication_refill', - type: 'date', - name: 'medication[refill]', - label: t('medications.form.refill'), - value: @medication.refill || nil, - info: t('medications.form.refill_hint'), - required: true, - dark: true - }, - { - id: 'medication_comments', - type: 'textarea', - name: 'medication[comments]', - label: t('comment.plural'), - value: @medication.comments || nil, - info: t('medications.form.comments_hint'), - dark: true - }, - { - id: 'medication_refill_reminder', - type: 'checkbox', - dark: true, - label: t('medications.refill_reminder'), - info: t('medications.form.refill_reminder_hint'), - name: 'medication[refill_reminder_attributes][active]', - checked: @medication&.refill_reminder&.active, - uncheckedValue: false, - value: true - }, - { - id: 'medication_take_medication_reminder', - type: 'checkbox', - dark: true, - label: t('common.daily_reminder'), - info: t('medications.form.daily_reminder_hint'), - name: 'medication[take_medication_reminder_attributes][active]', - checked: @medication&.take_medication_reminder&.active, - uncheckedValue: false, - value: true - }, - { - id: 'medication_refill_reminder_attributes_id', - name: 'medication[refill_reminder_attributes][id]', - type: 'hidden', - value: @medication&.refill_reminder&.id - }, - { - id: 'medication_take_medication_reminder_attributes_id', - name: 'medication[take_medication_reminder_attributes][id]', - type: 'hidden', - value: @medication&.take_medication_reminder&.id - } - ] + medication_unit_field('dosage') + ].concat(extra_fields).concat(reminder_fields) + end + + def medication_comments + { + type: 'textarea', + label: t('comment.plural'), + value: @medication.comments || nil, + info: t('medications.form.comments_hint') + }.merge(medication_basic_props('comments')) + end + + def medication_strength_unit + { + type: 'select', + value: @medication.strength_unit || t('medications.units.mg'), + options: [ + medication_type_unit_mg('strength'), + medication_type_unit_ml('strength') + ] + }.merge(medication_basic_props('strength_unit')) + end + + def medication_name + { + type: 'text', + label: t('common.name'), + value: @medication.name || nil, + info: t('categories.form.name_hint'), + required: true + }.merge(medication_basic_props('name')) + end + + def medication_refill + { + type: 'date', + label: t('medications.form.refill'), + value: @medication.refill || nil, + info: t('medications.form.refill_hint'), + required: true + }.merge(medication_basic_props('refill')) end - # rubocop:enable MethodLength - # rubocop:disable MethodLength def google_fields - common_fields.push( - id: 'medication_add_to_google_cal', + common_fields.push({ type: 'checkbox', - dark: true, label: t('medications.form.add_to_google_cal'), info: t('medications.form.google_cal_hint'), - name: 'medication[add_to_google_cal]', - checked: @medication.add_to_google_cal, + checked: @medication.add_to_google_cal || nil, uncheckedValue: false, value: true - ) + }.merge(medication_basic_props('add_to_google_cal'))) end - # rubocop:enable MethodLength - # rubocop:disable MethodLength def days_checkbox - week_days = 0.upto(6) - week_days.map do |i| + 0.upto(6).map do |i| { id: "medication_weekly_dosage_#{i}", type: 'checkbox', @@ -142,52 +172,53 @@ def days_checkbox } end end - # rubocop:enable MethodLength - # rubocop:disable MethodLength + def medication_type_unit_tablets(type) + { + id: "medication_#{type}_unit_tablets", + label: t('medications.units.tablets.other'), + value: t('medications.units.tablets.other') + } + end + + def medication_type_unit_mg(type) + { + id: "medication_#{type}_unit_mg", + label: t('medications.units.mg'), + value: t('medications.units.mg') + } + end + + def medication_type_unit_ml(type) + { + id: "medication_#{type}_unit_ml", + label: t('medications.units.ml'), + value: t('medications.units.ml') + } + end + def medication_unit_field(type) { - id: "medication_#{type}_unit", type: 'select', - name: "medication[#{type}_unit]", - dark: true, value: @medication["#{type}_unit"] || t('medications.units.tablets.other'), options: [ - { - id: "medication_#{type}_unit_tablets", - label: t('medications.units.tablets.other'), - value: t('medications.units.tablets.other') - }, - { - id: "medication_#{type}_unit_mg", - label: t('medications.units.mg'), - value: t('medications.units.mg') - }, - { - id: "medication_#{type}_unit_ml", - label: t('medications.units.ml'), - value: t('medications.units.ml') - } + medication_type_unit_tablets(type), + medication_type_unit_mg(type), + medication_type_unit_ml(type) ] - } + }.merge(medication_basic_props("#{type}_unit")) end - # rubocop:enable MethodLength - # rubocop:disable MethodLength def medication_field(type) { - id: "medication_#{type}", type: 'number', - name: "medication[#{type}]", label: t("medications.form.#{type}"), value: @medication[type.to_s] || nil, - required: true, info: t("medications.form.#{type}_hint"), placeholder: t("medications.form.#{type}_placeholder"), - dark: true - } + required: true + }.merge(medication_basic_props(type)) end - # rubocop:enable MethodLength end # rubocop:enable ModuleLength diff --git a/app/helpers/reminder_helper.rb b/app/helpers/reminder_helper.rb index c9ff86d607..a18bdfec4e 100644 --- a/app/helpers/reminder_helper.rb +++ b/app/helpers/reminder_helper.rb @@ -1,26 +1,28 @@ # frozen_string_literal: true - module ReminderHelper def print_reminders(data) - return '' unless active_reminders?(data) + return '' unless reminders?(data) names_arr = data.active_reminders.map(&:name) - reminders = format_reminders(names_arr) - reminders.html_safe + if data[:add_to_google_cal] + names_arr.push(t('medications.form.add_to_google_cal')) + end + format_reminders(names_arr).html_safe end private + def reminders?(data) + active_reminders?(data) || data[:add_to_google_cal] + end + def active_reminders?(data) data.methods.include?(:active_reminders) && data.active_reminders&.any? end def format_reminders(reminder_names) - reminders = '
' - reminders += '' - reminders += join_names(reminder_names) - reminders += '
' - reminders + reminders = join_names(reminder_names) + "
#{reminders}
" end def join_names(reminder_names) diff --git a/client/app/components/Form/index.jsx b/client/app/components/Form/index.jsx index 9fc095aabe..f14da44f0c 100644 --- a/client/app/components/Form/index.jsx +++ b/client/app/components/Form/index.jsx @@ -1,7 +1,8 @@ // @flow import React from 'react'; -import { Input, TYPES as INPUT_TYPES } from '../Input'; -import type { Props as InputProps } from '../Input'; +import { Input } from '../Input'; +import { TYPES as INPUT_TYPES } from '../Input/utils'; +import type { Props as InputProps } from '../Input/utils'; import { REQUIRES_DEFAULT } from '../Input/InputDefault'; import { QuickCreate } from '../../widgets/QuickCreate'; import type { Props as QuickCreateProps } from '../../widgets/QuickCreate'; diff --git a/client/app/components/Input/InputCheckbox.jsx b/client/app/components/Input/InputCheckbox.jsx index 28e83b9e0b..91c1722e7e 100644 --- a/client/app/components/Input/InputCheckbox.jsx +++ b/client/app/components/Input/InputCheckbox.jsx @@ -5,7 +5,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faQuestion } from '@fortawesome/free-solid-svg-icons'; import css from './Input.scss'; import globalCss from '../../styles/_global.scss'; -import type { Checkbox as Props } from './index'; +import type { Checkbox as Props } from './utils'; import { Tooltip } from '../Tooltip'; const displayUnchecked = (name: ?string, uncheckedValue: ?any) => ( diff --git a/client/app/components/Input/InputCheckboxGroup.jsx b/client/app/components/Input/InputCheckboxGroup.jsx index 8433bcc4e1..c4472fe981 100644 --- a/client/app/components/Input/InputCheckboxGroup.jsx +++ b/client/app/components/Input/InputCheckboxGroup.jsx @@ -1,6 +1,6 @@ // @flow import React from 'react'; -import type { Checkbox } from './index'; +import type { Checkbox } from './utils'; import { InputCheckbox } from './InputCheckbox'; export type Props = { diff --git a/client/app/components/Input/InputSelect.jsx b/client/app/components/Input/InputSelect.jsx index e950b64dae..d4bbff5c88 100644 --- a/client/app/components/Input/InputSelect.jsx +++ b/client/app/components/Input/InputSelect.jsx @@ -4,7 +4,7 @@ import renderHTML from 'react-render-html'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faCaretDown } from '@fortawesome/free-solid-svg-icons'; import css from './Input.scss'; -import type { Option } from './index'; +import type { Option } from './utils'; export type Props = { id: string, diff --git a/client/app/components/Input/InputTag.jsx b/client/app/components/Input/InputTag.jsx index b2d65bcb27..27e4cece31 100644 --- a/client/app/components/Input/InputTag.jsx +++ b/client/app/components/Input/InputTag.jsx @@ -1,7 +1,7 @@ // @flow import React from 'react'; import Autocomplete from 'react-autocomplete'; -import type { Checkbox } from './index'; +import type { Checkbox } from './utils'; import { InputCheckbox } from './InputCheckbox'; import inputCss from './Input.scss'; import css from './InputTag.scss'; diff --git a/client/app/components/Input/index.jsx b/client/app/components/Input/index.jsx index d349c491a1..4f0ce7a84c 100644 --- a/client/app/components/Input/index.jsx +++ b/client/app/components/Input/index.jsx @@ -8,100 +8,11 @@ import { InputCheckboxGroup } from './InputCheckboxGroup'; import { InputSelect } from './InputSelect'; import { InputTag } from './InputTag'; import { InputSwitch } from './InputSwitch'; -import { - InputDefault, - REQUIRES_DEFAULT, - DEFAULT_WITH_LABEL, -} from './InputDefault'; +import { InputDefault, REQUIRES_DEFAULT } from './InputDefault'; import { Accordion } from '../Accordion'; import css from './Input.scss'; - -export const TYPES = REQUIRES_DEFAULT.concat([ - 'textarea', - 'submit', - 'checkbox', - 'select', - 'checkboxGroup', - 'tag', - 'switch', -]); - -const REQUIRES_LABEL = DEFAULT_WITH_LABEL.concat([ - 'textarea', - 'select', - 'checkboxGroup', - 'tag', - 'switch', -]); - -const REQUIRED_POSSIBLE = DEFAULT_WITH_LABEL.concat([ - 'textarea', - 'checkboxGroup', -]); - -export type Option = { - id: string, - value: any, - label: string, -}; - -export type Checkbox = { - id: string, - name?: string, - value: any, - checked?: boolean, - uncheckedValue?: any, - label: string, - onChange?: Function, - info?: string, -}; - -export type Props = { - id: string, - type: | 'text' - | 'textarea' - | 'submit' - | 'checkbox' - | 'number' - | 'time' - | 'date' - | 'select' - | 'checkboxGroup' - | 'tag' - | 'hidden' - | 'switch', - name?: string, - label?: string, - placeholder?: string, - error?: boolean, - dark?: boolean, - small?: boolean, - large?: boolean, - value?: any, - readOnly?: boolean, - disabled?: boolean, - required?: boolean, - info?: string, - minLength?: number, - maxLength?: number, - onClick?: Function, - onChange?: Function, - checked?: boolean, - uncheckedValue?: any, - min?: number, - max?: number, - options?: Option[], - checkboxes?: Checkbox[], - onError?: Function, - accordion?: boolean, - myRef?: any, - accordionOpen?: boolean, - formNoValidate?: boolean, -}; - -export type State = { - error: boolean, -}; +import { TYPES, REQUIRES_LABEL, REQUIRED_POSSIBLE } from './utils'; +import type { Props, State } from './utils'; export class Input extends React.Component { constructor(props: Props) { @@ -325,19 +236,11 @@ export class Input extends React.Component { return null; }; - render() { + displayContent = () => { const { - type, - dark, - small, - large, - accordion, - label, - accordionOpen, - id, + dark, small, accordion, large, type, } = this.props; - if (!TYPES.includes(type)) return null; - const content = ( + return (
{ {this.displaySubmit()}
); + }; + + render() { + const { + type, + dark, + large, + accordion, + label, + accordionOpen, + id, + } = this.props; + if (!TYPES.includes(type)) return null; return accordion && label ? ( { dark={dark} large={large} > - {content} + {this.displayContent()} ) : ( - content + this.displayContent() ); } } diff --git a/client/app/components/Input/utils.js b/client/app/components/Input/utils.js new file mode 100644 index 0000000000..f3fb188057 --- /dev/null +++ b/client/app/components/Input/utils.js @@ -0,0 +1,89 @@ +// @flow +import { REQUIRES_DEFAULT, DEFAULT_WITH_LABEL } from './InputDefault'; + +export const TYPES = REQUIRES_DEFAULT.concat([ + 'textarea', + 'submit', + 'checkbox', + 'select', + 'checkboxGroup', + 'tag', + 'switch', +]); + +export const REQUIRES_LABEL = DEFAULT_WITH_LABEL.concat([ + 'textarea', + 'select', + 'checkboxGroup', + 'tag', + 'switch', +]); + +export const REQUIRED_POSSIBLE = DEFAULT_WITH_LABEL.concat([ + 'textarea', + 'checkboxGroup', +]); + +export type Option = { + id: string, + value: any, + label: string, +}; + +export type Checkbox = { + id: string, + name?: string, + value: any, + checked?: boolean, + uncheckedValue?: any, + label: string, + onChange?: Function, + info?: string, +}; + +export type Props = { + id: string, + type: | 'text' + | 'textarea' + | 'submit' + | 'checkbox' + | 'number' + | 'time' + | 'date' + | 'select' + | 'checkboxGroup' + | 'tag' + | 'hidden' + | 'switch', + name?: string, + label?: string, + placeholder?: string, + error?: boolean, + dark?: boolean, + small?: boolean, + large?: boolean, + value?: any, + readOnly?: boolean, + disabled?: boolean, + required?: boolean, + info?: string, + minLength?: number, + maxLength?: number, + onClick?: Function, + onChange?: Function, + checked?: boolean, + uncheckedValue?: any, + min?: number, + max?: number, + options?: Option[], + checkboxes?: Checkbox[], + onError?: Function, + accordion?: boolean, + myRef?: any, + accordionOpen?: boolean, + formNoValidate?: boolean, +}; + +export type State = { + error: boolean, +}; diff --git a/client/app/components/Tag/__tests__/Tag.spec.jsx b/client/app/components/Tag/__tests__/Tag.spec.jsx index 8fe1d8e06f..4e010e1fc8 100644 --- a/client/app/components/Tag/__tests__/Tag.spec.jsx +++ b/client/app/components/Tag/__tests__/Tag.spec.jsx @@ -13,12 +13,16 @@ describe('Tag', () => { it('supports normal class', () => { const tag = shallow(); - expect(tag.prop('className')).toEqual(expect.stringContaining(css.normal)); + expect(tag.prop('className')).toEqual( + expect.stringContaining(css.normal), + ); }); it('supports secondary class', () => { const tag = shallow(); - expect(tag.prop('className')).toEqual(expect.stringContaining(css.secondary)); + expect(tag.prop('className')).toEqual( + expect.stringContaining(css.secondary), + ); }); }); }); diff --git a/client/app/mocks/InputMocks.jsx b/client/app/mocks/InputMocks.jsx index 5726cff029..5f8eee2a5d 100644 --- a/client/app/mocks/InputMocks.jsx +++ b/client/app/mocks/InputMocks.jsx @@ -1,7 +1,7 @@ // @flow import React from 'react'; import { Input } from '../components/Input'; -import type { Props } from '../components/Input'; +import type { Props } from '../components/Input/utils'; const options = [ { id: 'some-option-one-id', value: 1, label: 'Some Option One' }, diff --git a/client/app/widgets/QuickCreate/index.jsx b/client/app/widgets/QuickCreate/index.jsx index db5c4cd135..154f1eb2ec 100644 --- a/client/app/widgets/QuickCreate/index.jsx +++ b/client/app/widgets/QuickCreate/index.jsx @@ -2,7 +2,7 @@ import React from 'react'; import { Modal } from '../../components/Modal'; import { Input } from '../../components/Input'; -import type { Checkbox } from '../../components/Input'; +import type { Checkbox } from '../../components/Input/utils'; import { Utils } from '../../utils'; import css from './QuickCreate.scss'; import { DynamicForm } from '../../components/Form/DynamicForm'; diff --git a/client/app/widgets/Resources/__tests__/Resources.spec.jsx b/client/app/widgets/Resources/__tests__/Resources.spec.jsx index d0f359448f..3fda4915d4 100644 --- a/client/app/widgets/Resources/__tests__/Resources.spec.jsx +++ b/client/app/widgets/Resources/__tests__/Resources.spec.jsx @@ -48,7 +48,9 @@ describe('Resources', () => { .simulate('click'); expect(wrapper.find('.checkboxLabel').text()).toEqual(id); expect(wrapper.find('.resource').length).toEqual(1); - expect(wrapper.find('.tag').findWhere(t => t.text() === id).length).toEqual(1); + expect(wrapper.find('.tag').findWhere(t => t.text() === id).length).toEqual( + 1, + ); }); it('unfilters when tag unselected', () => { diff --git a/spec/features/user_creates_a_medication_spec.rb b/spec/features/user_creates_a_medication_spec.rb index 1a8e0ea2f4..bb5526cca9 100644 --- a/spec/features/user_creates_a_medication_spec.rb +++ b/spec/features/user_creates_a_medication_spec.rb @@ -30,8 +30,8 @@ end it 'creates a new medication' do - expect(CalendarUploader).to_not receive(:new) find('#submit').click + expect(CalendarUploader).to_not receive(:new) within '.pageTitle' do expect(page).to have_content(name) @@ -44,10 +44,10 @@ context 'with reminders checked' do it 'activates reminders' do - find('#medication_refill_reminder').click - find('#medication_take_medication_reminder').click - expect(CalendarUploader).to_not receive(:new) + find('#medication_refill_reminder_attributes').click + find('#medication_take_medication_reminder_attributes').click find('#submit').click + expect(CalendarUploader).to_not receive(:new) expect(find('.pageTitle')).to have_content(name) expect(medication.take_medication_reminder.active?).to be true expect(medication.refill_reminder.active?).to be true @@ -60,8 +60,8 @@ end it 'activates reminders' do - find('#medication_refill_reminder').click - find('#medication_take_medication_reminder').click + find('#medication_refill_reminder_attributes').click + find('#medication_take_medication_reminder_attributes').click find('#medication_add_to_google_cal').click expect(CalendarUploader).to receive_message_chain(:new, :upload_event) find('#submit').click @@ -78,7 +78,7 @@ end it 'redirects to sign in' do - find('#medication_refill_reminder').click + find('#medication_refill_reminder_attributes').click find('#medication_add_to_google_cal').click find('#submit').click expect(find('#new_user')).to be_present diff --git a/spec/helpers/medications_helper_spec.rb b/spec/helpers/medications_helper_spec.rb index 2350defeff..cb4d8968c6 100644 --- a/spec/helpers/medications_helper_spec.rb +++ b/spec/helpers/medications_helper_spec.rb @@ -33,7 +33,7 @@ def get_field(field_name) let(:medication) { create(:medication, :with_refill_reminder, user: current_user) } it 'sets a medication refill reminder' do @medication = medication - expect(get_field('medication_refill_reminder')[:value]).to eq(true) + expect(get_field('medication_refill_reminder_attributes')[:value]).to eq(true) expect(get_field('medication_refill_reminder_attributes_id')[:value]).to eq(medication.refill_reminder.id) end end @@ -42,7 +42,7 @@ def get_field(field_name) let(:medication) { create(:medication, :with_daily_reminder, user: current_user) } it 'sets a medication take medication reminder' do @medication = medication - expect(get_field('medication_take_medication_reminder')[:value]).to eq(true) + expect(get_field('medication_take_medication_reminder_attributes')[:value]).to eq(true) expect(get_field('medication_take_medication_reminder_attributes_id')[:value]).to eq(medication.take_medication_reminder.id) end end diff --git a/spec/helpers/reminder_helper_spec.rb b/spec/helpers/reminder_helper_spec.rb index a16f9e3052..d156427dfe 100644 --- a/spec/helpers/reminder_helper_spec.rb +++ b/spec/helpers/reminder_helper_spec.rb @@ -5,7 +5,7 @@ name1, name2 ] - let(:user) {FactoryBot.create(:user)} + let(:user) { create(:user_oauth) } describe '#active_reminders?' do it 'returns false when data has no active_reminder method' do @@ -13,12 +13,12 @@ end it 'returns false when data has no active reminder' do - medication = FactoryBot.create(:medication, user_id: user.id) + medication = create(:medication, user_id: user.id) expect(active_reminders?(medication)).to be false end it 'returns true when data is good' do - medication = FactoryBot.create(:medication, :with_daily_reminder, user_id: user.id) + medication = create(:medication, :with_daily_reminder, user_id: user.id) expect(active_reminders?(medication)).to be true end end @@ -26,7 +26,7 @@ describe '#join_names' do it 'correctly joins reminder names with proper connector' do reminders = join_names(reminder_names) - expected_string = 'name1' + I18n.translate('support.array.words_connector') + 'name2' + expected_string = "name1#{t('support.array.words_connector')}name2" expect(reminders).to eq(expected_string) end end @@ -45,12 +45,39 @@ expect(reminders).to eq('') end - it 'returns correct html when data has reminders' do - medication = FactoryBot.create(:medication, :with_daily_reminder, user_id: user.id) + it 'returns correct html when data has a daily reminder' do + medication = create(:medication, :with_daily_reminder, user_id: user.id) reminders = print_reminders(medication) - expected_html = '
' + I18n.t('common.daily_reminder') + '
' + expected_html = "
#{t('common.daily_reminder')}
" + expect(reminders).to eq(expected_html) + end + + it 'returns correct html when data has a refill reminder' do + medication = create(:medication, :with_refill_reminder, user_id: user.id) + reminders = print_reminders(medication) + expected_html = "
#{t('medications.refill_reminder')}
" + expect(reminders).to eq(expected_html) + end + + it 'returns correct html when data has a daily reminder and refill reminder' do + medication = create(:medication, :with_both_reminders, user_id: user.id) + reminders = print_reminders(medication) + expected_html = "
#{t('medications.refill_reminder')}, #{t('common.daily_reminder')}
" expect(reminders).to eq(expected_html) end - end + it 'returns correct html when data has a Google calendar reminder' do + medication = create(:medication, add_to_google_cal: true, user_id: user.id) + reminders = print_reminders(medication) + expected_html = "
#{t('medications.form.add_to_google_cal')}
" + expect(reminders).to eq(expected_html) + end + + it 'returns correct html when data has all reminders' do + medication = create(:medication, :with_both_reminders, add_to_google_cal: true, user_id: user.id) + reminders = print_reminders(medication) + expected_html = "
#{t('medications.refill_reminder')}, #{t('common.daily_reminder')}, and #{t('medications.form.add_to_google_cal')}
" + expect(reminders).to eq(expected_html) + end + end end