Skip to content

Class Component issues #1058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
navidshad opened this issue Oct 16, 2021 · 8 comments
Closed

Class Component issues #1058

navidshad opened this issue Oct 16, 2021 · 8 comments

Comments

@navidshad
Copy link

I use typescript and vue-class-component on vue3. when I want to change the component data I will get an error:

await wrapper.setData({ showNextButton: true });
// TypeError: Cannot add property showNextButton, object is not extensible

This is the component:

import { Vue, Options, prop } from "vue-class-component";
import { QuizMode } from "@/common/models/config";

import MarkdownComponent from "@/common/components/layout/Markdown.vue";
import ButtonCheckComponent from "./ButtonCheck.vue";

type SubmitFunction = (configNumber?: number) => Promise<boolean>;

class Props {
  text = prop<string>({ required: true });
  subtext = prop<string>({ default: "" });
  type = prop<"multiple_choice" | "info">({ required: true });
  submitFunction = prop<SubmitFunction>({ required: true });
}

@Options({
  components: {
    MarkdownComponent,
    ButtonCheckComponent,
  },
  inject: ["quiz_mode", "staging", "staging_message"],
  emits: ["next"],
})
export default class ConfigQuestion extends Vue.with(Props) {
  quiz_mode!: QuizMode;
  staging!: boolean;
  staging_message!: string;

  checking = false;
  disabledCheckButton = false;
  disabledNextButton = false;

  status: string | null = null;
  showNextButton = false;

  check(configNumber?: number) {
    this.checking = true;
    this.disabledCheckButton = true;

    this.submitFunction(configNumber)
      .then((success) => {
        this.status = success ? "correct" : "wrong";
        this.showNextButton = true;
        console.log('showNextButton', this.showNextButton);
        
      })
      .finally(() => {
        this.checking = false;
      });
  }

  next() {
    if (this.disabledNextButton) return;

    this.disabledNextButton = true;
    this.$emit("next");
  }
}

And this is the test script:

import { expect } from 'chai';
import { shallowMount, mount, VueWrapper, flushPromises } from '@vue/test-utils';

import ConfigQuestionComponent from '@/modules/lab/components/ConfigQuestion.vue';
import { ComponentPublicInstance, nextTick } from 'vue';

describe("Lab Config Component", () => {

	let is_submitFUnctionRun = false;
	let wrapper: VueWrapper<ComponentPublicInstance>;

	before(() => {

		wrapper = shallowMount(ConfigQuestionComponent, {
			props: {
				text: "This is a config question",
				subtext: "this is a subline",
				type: "info",
				submitFunction: () => new Promise<boolean>(() => {
					is_submitFUnctionRun = true;
					return is_submitFUnctionRun;
				}),
			},
			global: {
				provide: {
					quiz_mode: 'Lab',
					staging: false,
					staging_message: '',
				}
			}
		});

	})

	it("Expect to emit next event", async () => {

		await wrapper.setData({ showNextButton: true });

		//expect(wrapper.find('#next').exists()).eq(true)
	})
})
@navidshad navidshad changed the title Get 'object is not extensible' on setData Get 'TypeError: Cannot add property, object is not extensible' on setData method Oct 17, 2021
@navidshad navidshad changed the title Get 'TypeError: Cannot add property, object is not extensible' on setData method TypeError: Cannot add property, object is not extensible on setData method Oct 17, 2021
@CristianoGil
Copy link

+1

1 similar comment
@spencermtheodo
Copy link

+1

@afontcu afontcu transferred this issue from vuejs/vue-test-utils Nov 4, 2021
@vjself
Copy link

vjself commented Nov 15, 2021

+1

1 similar comment
@mike-merilaht
Copy link

+1

@lmiller1990
Copy link
Member

Thanks for the bug report. I am not even sure if this is something we can fix without modifying Vue Class Component.

For anyone following/waiting on a fix: happy to accept a PR, but I won't be able to prioritise this issue in the near future. The class component appears not to be actively maintained, and is not generally recommended by Evan: vuejs/core#4744 (comment)

If you feel strongly about this, I can help review or give you some pointers. Isolating the issue is probably not hard;

Clone the repo, add a class component example and write code in mergeDeep until it works. If it's not possible, we might need to add a feature to the class component repo to support setData.

@tsiotska
Copy link

As temporary workaround i use
wrapper.vm.changedData['firstName'] = 'foo'

@lmiller1990 lmiller1990 changed the title TypeError: Cannot add property, object is not extensible on setData method Class Component issues Apr 19, 2022
@lmiller1990
Copy link
Member

Related: #572

@cexbrayat
Copy link
Member

As vue-class-component has been deprecated for a long time (see https://github.com/vuejs/vue-class-component?tab=readme-ov-file#deprecated-vue-class-component), I think we should close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants