Skip to content

Instantly share code, notes, and snippets.

@joboccara
joboccara / product_controller_test.rb
Last active June 9, 2022 12:53
Software design training: tests for iteration on data model
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']
@joboccara
joboccara / product_prices_controller_test.rb
Last active June 9, 2022 09:11
Software design training: tests for iteration on prices
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
@joboccara
joboccara / design_training_external_images.rb
Last active June 9, 2022 08:58
Design training: Test and external api for storing images
################## 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})
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>
);
const onClickMemo = React.useCallback(
() => window.alert(`clickMemo, count: ${count}`),
[]
);
const countRef = React.useRef(count);
countRef.current = count;
const onClickMemo = React.useCallback(
() => window.alert(`clickMemo, count: ${countRef.current}`),
[]
);
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 (
const MemoComponent = React.memo(({ onClick }) => {
return (
<>
<Button onClick={onClick}>Click on the memo</button>
</>
);
});
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} />
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>
);
};