Is there a way to mock a method from an object within my UnderTest class?
When the Update
method is triggered by an event from a child component, I need to mock the service.saveNewElement(data)
method in order to test data in the then()
block.
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import { Service } from 'secret';
import ROUTES from '../../../../router/routes.const';
@Component({
name: 'UnderTest',
template: require('./underTest.component.html'),
})
export default class UnderTest extends Vue {
private service: Service;
constructor() {
super();
this.service = new Service();
}
public update(data: any): void {
this.service
.saveNewElement(data)
.then(() => {
//stuff to do
})
.catch(() => {
this.$notify.error('Oops!, sth went wrong...');
});
}
}
</script>
Any suggestions on how to accomplish this?
EDIT:
I tried the suggestion from @slideshowp2 but encountered another issue.
I used the following code snippet in my test file:
jest.mock('../../services/customer.service',() => ({
constructor: jest.fn(() => {}),
saveNewCustomer: jest.fn(() => Promise.resolve({ data: 3 }))
}));
This resulted in an error:
[Vue warn]: Error in data(): "TypeError: service.Service is not a constructor"
Can anyone provide guidance on resolving this?