Presentational
وإلى Redux. حالياً ، لديك Redux متصل ، لكنك تتعامل مع الحالة محليًا داخل مكون Presentational
. Presentational
أولاً ، قم بإزالة خاصية messages
في state
المحلية. ستتم إدارة هذه الرسائل بواسطة Redux. بعد ذلك ، قم بتعديل طريقة submitMessage()
بحيث يتم إرسال submitNewMessage()
من this.props
، وتمرير الإدخال الحالي للرسالة من state
المحلية كوسيطة. نظرًا لأنك أزلت messages
من الحالة المحلية ، فأزل خاصية messages
من المكالمة إلى this.setState()
هنا أيضًا. وأخيرًا ، قم بتعديل طريقة العرض render()
بحيث يتم تعيينها على الرسائل المستلمة من props
بدلاً من state
. بمجرد إجراء هذه التغييرات ، سيستمر التطبيق في العمل كما هو ، باستثناء أن يقوم Redux بإدارة الحالة. يوضح هذا المثال أيضا كيف يمكن أن يكون المكون المحلي state
: مكون الخاص بك لا يزال يتابع إدخال المستخدم محليا في الخاصة به state
. يمكنك أن ترى كيف يوفر Redux إطارًا مفيدًا لإدارة الدولة أعلى React. لقد حققت النتيجة نفسها باستخدام حالة React المحلية فقط في البداية ، وهذا ممكن عادة باستخدام تطبيقات بسيطة. ومع ذلك ، عندما تصبح تطبيقاتك أكبر وأكثر تعقيدًا ، كذلك فإن إدارة الولاية لديك ، وهذه هي المشكلة التي يحلها Redux. AppWrapper
should render to the page.");'
- text: يجب أن يقوم مكون Presentational
بعرض عناصر h2
و input
و button
و ul
.
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); return mockedComponent.find("Presentational").length === 1; })(), "The Presentational
component should render an h2
, input
, button
, and ul
elements.");'
- text: ''
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const PresentationalComponent = mockedComponent.find("Presentational"); return ( PresentationalComponent.find("div").length === 1 && PresentationalComponent.find("h2").length === 1 && PresentationalComponent.find("button").length === 1 && PresentationalComponent.find("ul").length === 1 ); })(), "The Presentational
component should render an h2
, input
, button
, and ul
elements.");'
- text: يجب أن يتلقى المكون Presentational
messages
من مخزن Redux كدعم.
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const PresentationalComponent = mockedComponent.find("Presentational"); const props = PresentationalComponent.props(); return Array.isArray(props.messages); })(), "The Presentational
component should receive messages
from the Redux store as a prop.");'
- text: يجب أن يتلقى المكون Presentational
منشئ إجراء submitMessage
كدعم.
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const PresentationalComponent = mockedComponent.find("Presentational"); const props = PresentationalComponent.props(); return typeof props.submitNewMessage === "function"; })(), "The Presentational
component should receive the submitMessage
action creator as a prop.");'
- text: يجب أن تحتوي حالة المكون Presentational
على خاصية واحدة ، input
، والتي تتم تهيئتها إلى سلسلة فارغة.
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const PresentationalState = mockedComponent.find("Presentational").instance().state; return typeof PresentationalState.input === "string" && Object.keys(PresentationalState).length === 1; })(), "The state of the Presentational
component should contain one property, input
, which is initialized to an empty string.");'
- text: ''
testString: 'async () => { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const testValue = "__MOCK__INPUT__"; const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 100)); const causeChange = (c, v) => c.find("input").simulate("change", { target: { value: v }}); let initialInput = mockedComponent.find("Presentational").find("input"); const changed = () => { causeChange(mockedComponent, testValue); return waitForIt(() => mockedComponent )}; const updated = await changed(); const updatedInput = updated.find("Presentational").find("input"); assert(initialInput.props().value === "" && updatedInput.props().value === "__MOCK__INPUT__", "Typing in the input
element should update the state of the Presentational
component."); }; '
- text: يجب أن يؤدي إرسال submitMessage
على مكون Presentational
تحديث مخزن Redux ومسح الإدخال في الحالة المحلية.
testString: 'async () => { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 100)); let beforeProps = mockedComponent.find("Presentational").props(); const testValue = "__TEST__EVENT__INPUT__"; const causeChange = (c, v) => c.find("input").simulate("change", { target: { value: v }}); const changed = () => { causeChange(mockedComponent, testValue); return waitForIt(() => mockedComponent )}; const clickButton = () => { mockedComponent.find("button").simulate("click"); return waitForIt(() => mockedComponent )}; const afterChange = await changed(); const afterChangeInput = afterChange.find("input").props().value; const afterClick = await clickButton(); const afterProps = mockedComponent.find("Presentational").props(); assert(beforeProps.messages.length === 0 && afterChangeInput === testValue && afterProps.messages.pop() === testValue && afterClick.find("input").props().value === "", "Dispatching the submitMessage
on the Presentational
component should update Redux store and clear the input in local state."); }; '
- text: يجب أن يقوم المكون Presentational
بعرض messages
من مخزن Redux.
testString: 'async () => { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 100)); let beforeProps = mockedComponent.find("Presentational").props(); const testValue = "__TEST__EVENT__INPUT__"; const causeChange = (c, v) => c.find("input").simulate("change", { target: { value: v }}); const changed = () => { causeChange(mockedComponent, testValue); return waitForIt(() => mockedComponent )}; const clickButton = () => { mockedComponent.find("button").simulate("click"); return waitForIt(() => mockedComponent )}; const afterChange = await changed(); const afterChangeInput = afterChange.find("input").props().value; const afterClick = await clickButton(); const afterProps = mockedComponent.find("Presentational").props(); assert(beforeProps.messages.length === 0 && afterChangeInput === testValue && afterProps.messages.pop() === testValue && afterClick.find("input").props().value === "" && afterClick.find("ul").childAt(0).text() === testValue, "The Presentational
component should render the messages
from the Redux store."); }; '
```