state
في تطبيقات React بطرق أكثر تعقيدًا من تلك التي شاهدتها حتى الآن. أحد الأمثلة على ذلك هو مراقبة حالة القيمة ، ثم عرض واجهة المستخدم بشكل مشروط بناءً على هذه القيمة. هناك عدة طرق مختلفة لإنجاز هذا ، ويظهر محرر الشفرة طريقة واحدة. MyComponent
should return a div
element which contains a button
.");'
- text: يجب تهيئة حالة MyComponent
مع تعيين خاصية visibility
إلى false
.
testString: 'assert.strictEqual(Enzyme.mount(React.createElement(MyComponent)).state("visibility"), false, "The state of MyComponent
should initialize with a visibility
property set to false
.");'
- text: ''
testString: 'async () => { const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250)); const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); const first = () => { mockedComponent.setState({ visibility: false }); return waitForIt(() => mockedComponent.state("visibility")); }; const second = () => { mockedComponent.find("button").simulate("click"); return waitForIt(() => mockedComponent.state("visibility")); }; const third = () => { mockedComponent.find("button").simulate("click"); return waitForIt(() => mockedComponent.state("visibility")); }; const firstValue = await first(); const secondValue = await second(); const thirdValue = await third(); assert(!firstValue && secondValue && !thirdValue, "Clicking the button element should toggle the visibility
property in state between true
and false
."); }; '
```