EnCiv / undebate-ssp

Undebate Self Service Portal- Web portal where election administrators of democratically run organizations create undebates for their candidates and voters
Other
6 stars 9 forks source link

getElectionStatusMethods #105

Closed ddfridley closed 2 years ago

ddfridley commented 2 years ago

The interface/pattern to be compatible with useMethods (see below)

app/lib/get-election-status-methods.js


// pure functions should be declared outside the object
const checkDateCompleted = obj => {
        // eslint-disable-next-line no-restricted-syntax
        for (const key in obj) {
            if (obj[key].date !== '') {
                return true
            }
        }
        return false
    }

// state is the electionObj
// dispatch would be called by a method that wanted to change the state
const getElectionStatusMethods=(dispatch, state)=>({
        checkDateCompleted: checkDateCompleted,
        checkTimelineCompleted: ()=>{
            return (
            checkDateCompleted(state?.timeline?.moderatorDeadlineReminderEmails) &&
            checkDateCompleted(state?.timeline?.moderatorSubmissionDeadline) &&
            checkDateCompleted(state?.timeline?.candidateDeadlineReminderEmails) &&
            checkDateCompleted(state?.timeline?.candidateSubmissionDeadline)
            )
        },
        ....
})

export default getElectionStatusMethods
import getElectionStatusMethods from ../app/lib/get-election-status-methods

const electionOM = useMethods(
            (dispatch, state) => ({
                ...getElectionStatusMethods(dispatch, state),
                upsert(obj) {
                    dispatch(merge({}, state, obj, { _count: state._count + 1 }))
                },
                ...

app/lib/tests/get-election-status-methods-[method-name].js where [method-name] is one of the methods in the file see cloudinary-urls.test.js for an example of writing a test. see Testing for more on tests

kamui-fin commented 2 years ago

How do you interface with the status methods from a jest test? Since it's a react hook (useMethods), it isn't callable inside of a normal unit test.

ddfridley commented 2 years ago

@kamui-fin a fair point. useMethods takes a function, which returns an object that has all the functions as properties. Perhaps the jest test should call that function, passing it's own dispatch function and state. And then test the functions. Or the functions could be exported from where they are and imported by the jest test.