This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class ProductsControllerTest < ActionDispatch::IntegrationTest | |
test 'gets a book with its details' do | |
book = # Create book with title: 'Item 1', isbn: '1', purchase_price: 42, is_hot: true | |
get product_url(book[:id]) | |
res = response.parsed_body | |
assert_equal 'Item 1', res['title'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "test_helper" | |
require "csv" | |
class ProductPricesControllerTest < ActionDispatch::IntegrationTest | |
teardown do | |
Timecop.return | |
end | |
test 'prices books at +25% margin' do | |
book1 = # Create book with title: 'Title of Book1', isbn: '1', purchase_price: 12, is_hot: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################## TEST ###################### | |
# test/controllers/products_controller_test.rb | |
############################################## | |
test 'gets an image with its details from an external service' do | |
begin | |
ENV['IMAGES_FROM_EXTERNAL_SERVICE'] = 'true' | |
IMAGE_EXTERNAL_ID = 42 | |
ImageExternalService.expects(:upload_image_details).with(width: 800, height: 600).returns(IMAGE_EXTERNAL_ID) | |
ImageExternalService.expects(:get_image_details).with(IMAGE_EXTERNAL_ID).returns({width: 800, height: 600}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MemoComponent = React.memo(({ onClick }) => { | |
const buttonRef = React.useRef(null); | |
React.useEffect(() => { | |
const buttonDOM = buttonRef.current; | |
buttonDOM.addEventListener("click", onClick); | |
return () => buttonDOM.removeEventListener("click", onClick); | |
}, []); | |
return ( | |
<Button ref={buttonRef}>Click on the memo</button> | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const onClickMemo = React.useCallback( | |
() => window.alert(`clickMemo, count: ${count}`), | |
[] | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const countRef = React.useRef(count); | |
countRef.current = count; | |
const onClickMemo = React.useCallback( | |
() => window.alert(`clickMemo, count: ${countRef.current}`), | |
[] | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CountContext = React.createContext(null); | |
const CountContextProvider = ({ children }) => { | |
const [count, setCount] = React.useState(0); | |
const countRef = React.useRef(count); // more on this in the next section | |
countRef.current = count; | |
const onClickMemo = React.useCallback( | |
() => window.alert(`clickMemo, count: ${countRef.current}`), | |
[] | |
); | |
return ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MemoComponent = React.memo(({ onClick }) => { | |
return ( | |
<> | |
<Button onClick={onClick}>Click on the memo</button> | |
</> | |
); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CountContext = React.createContext(null); | |
const CountContextProvider = ({ children }) => { | |
const [count, setCount] = React.useState(0); | |
const onClickMemo = () => | |
window.alert(`clickMemo, count: ${count}`); | |
return ( | |
<CountContext.Provider value={{ count, setCount }}> | |
<GrandParent>{children}</GrandParent> | |
<MemoComponent onClick={onClickMemo} /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Child = () => { | |
const { count, setCount } = React.useContext(CountContext); | |
return ( | |
<div> | |
<Button onClick={() => setCount((count) => count + 1)}> | |
Call setCount from context with same value | |
</button> | |
</div> | |
); | |
}; |
NewerOlder