I've written a test for vue3 for one of my components:
import { describe} from 'vitest';
import { shallowMount } from '@vue/test-utils';
import ProgressBar from '@/components/ProgressBar.vue';
const messages = {
"en-US" : {
strings: {
a: "A",
b: "B",
c: "C"
}
},
"fr-CA": {
strings: {
a: "AA",
b: "BB",
c: "CC"
}
}
};
const locale = "en-US";
try {
describe("ProgressBar.vue", () => {
function stepClick() {
console.log("step click");
}
const stepData = {
steps: [
{ index: 1, completed: false, clickActive: false },
{ index: 2, completed: false, clickActive: false },
{ index: 3, completed: false, clickActive: false }
],
currentIndex: 1
};
const component = shallowMount(ProgressBar, {
props: {
stepDataSource: "stepData",
onClick: stepClick,
locPage: "strings",
locKeys:['a', 'b', 'c' ]
},
provide: {
stepData() { return stepData }
},
mocks: {
'$t': (key) => {
const params = key.split('.');
return messages[locale][params[0]][params[1]];
}
}
});
});
} catch (e) {
console.log(e);
}
The component injects the specified data in properties:
stepDataSource: "stepData"
It is loaded by a method in the component:
async getStepData() {
console.log('getStepData');
const stepData = inject(this.stepDataSource) as StepData;
this.data.stepData = stepData;
const instance = getCurrentInstance();
instance?.proxy?.$forceUpdate();
},
Which works in normal running but it doesn't seem to get passed by the test. AFAIK, my mock of locale is working.
So what am I doing wrong to inject the step data for the test? I understand that I don't actually have any tests yet, but I need to get it to mount first.