ax ios запрос в функции debounce не выполняется при модульном тестировании с Vue. js - PullRequest
0 голосов
/ 05 мая 2020

У меня есть компонент, и у него есть наблюдатель.

watch: {
        newName: function() {
            clearTimeout(this.nameTimeout);
            if (this.newName.replace(/-|\s/g, '').length === 0) {
                this.newName = '';
            }
            this.nameExists = false;
            if (!this.$v.$invalid && this.newName) {
                this.nameProcessing = true;
             //    this.checkExistence();
               this.nameTimeout = setTimeout(this.checkExistence, 200);
            }
        },
    },

И я создал метод, который отправляет запрос на получение, чтобы проверить, существует ли имя или нет.

methods: {
        checkExistence: function() {
            let r = this.$axios.single.get(this.$routing.generate('check_name_exists', 
                {name: this.newName}))
                .then((response) => {

                    if (HTTP_OK === response.status) {
                        this.nameExists = response.data.exists;
                        this.nameProcessing = false;
                    }
                }).catch().then(() => 'hello');
            console.log(Promise.resolve(r));
            return r;
        },

Когда я хочу его протестировать, он не удался с данной ошибкой :

TypeError: undefined не является объектом (оценка 'this. $ Ax ios .single')

Я пробовал nexttick, sinon fake timer et c .. Я не мог это решить.

Когда я помещаю метод checkExistence за пределы функции debounce, тест проходит ....

Как это можно сделать?

...