Skip to content

Unit testing a metaInfo() statement #493

@terazus

Description

@terazus

Hello,

Let's say I have this vueJS component:

export default {
        name: "Record",
        id: 'ABA',
        methods: {
            // @vuese
            // Method to build and return the page title to be included as a metadata
            getTitle: function(){
                return 'MywebSite  | ' + this.$route.params['id']
            }
        },
        // @vuese
        // set the meta-data of the page
        metaInfo() {
            return {
                title: this.getTitle()
            }
        }
    }

How would you unit test that the meta title of the page is correctly set? I couldn't fiund the way to trigger the function within the following test:

import { shallowMount } from '@vue/test-utils'
import Record from './Record.vue'

const $route = {
    path: '/',
    params: {
        id: '120'
    }
};

describe('Record.vue', function() {

    // Set up the wrapper
    let wrapper;
    beforeEach(() => {
        wrapper = shallowMount(Record, {
            mocks: {$route}
        });
    });
    const title = 'MywebSite  | 120 ' ;


    it('can be instantiated', () => {
        expect(wrapper.name()).toMatch('Record');
        expect(wrapper.attributes('id')).toMatch('ABA');
    });

    it('has a getTitle() method that returns the page title', () => {
        expect(wrapper.vm.getTitle()).toBe(title);
    });

    it('has its meta title correctly set', () => {
        // test metaInfo() here
    });
})

Thank you very much :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions