D
D
dicem2020-04-14 12:42:33
Software testing
dicem, 2020-04-14 12:42:33

Why does jest testing of a vue component fail?

Hello, I have a component with a method:

scrollToParent() {
            const id = _get(this, 'comment.attributes.parent_id'); // lodash get
            const parentId = `#comment-${id}`;
            this.$emit('scrollToParent', parentId, id);
},

emit is listened to in parent component and test for it
<Tag ... @scrollToParent="scrollToParent" ... />
it('scrollToParent - Должен вызвать emit со свойством scrollToParent и payload', () => {
            component.vm.$emit = jest.fn();
            component.setProps({
                comment: {
                    attributes: {
                        parent_id: 3,
                    },
                },
            });
            component.vm.scrollToParent();
            expect(component.vm.$emit).toBeCalledWith('scrollToParent', '#comment-3', 3);
});

The test explicitly specifies parent_id by the setProps method, but for some reason the test ends up with parent_id being undefined. Why is this happening?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question