Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
Test element in module with vue-utils

Here is my problem that I can't solve:

I would like to test the yearsDropdownItems function of my MonthlyFoodCostModal component. However, I keep getting this error:

TypeError: Cannot read properties of undefined (reading 'outlet_start_date')

How can I set a value to activeOutlet?

Here is my component code :

import { createNamespacedHelpers } from "vuex";


const { mapGetters: userGetters } = createNamespacedHelpers("user");

export default {
    computed: {
        yearsDropdownItems,

        ...userGetters([
            "activeOutlet"
        ])
    },
};

function yearsDropdownItems() {
    const outletStartYear = moment(this.activeOutlet.outlet_start_date).year(),
        outletEndYear = moment(this.activeOutlet.outlet_data_validity).year(),
        years = this.$utils.range(outletStartYear, outletEndYear);

    return this.$utils.sortDesc(years);
}

Here is my test code :

import { mount } from "@vue/test-utils"
import MonthlyFoodCostModal from "@/components/monthlyFoodCostModal/monthlyFoodCostModal.js"
import User from "@/store/Module/User/userModule.js"
import Vue from "vue"
import Vuex from "vuex"

Vue.use(Vuex)

let state;
let store;

describe('Unit test MonthlyFoodCostModal.vue', () => {

    beforeEach(() => {
        state = {}
        store = new Vuex.Store({
            modules: {
              User: {
                state,
                getters: User.getters,
                namespaced: true
              }
            }
        })
    })
    
    it('test yearsDropdownItems method', async() => {
      const wrapper = mount(MonthlyFoodCostModal, {store})
      expect(wrapper.vm.yearsDropdownItems).toBe("?")
    })

})
I didn't put anything in the "expect" part because the error happens before the test is viewed.

My module behaves as follows:

const initialState = () => ({
    user: {
        firstName: "",
        lastName: "",
        role: ""
    },
    properties: [],
    activeProperty: null,
    activeOutlet: null,
    selectedDevicesFilter: [],
});

export default {
    namespaced: true,
  state: initialState(),
  getters: {
    activeOutlet: state => state.activeOutlet
  }
}

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!