Skip to content

Instantly share code, notes, and snippets.

View thisismydesign's full-sized avatar

Csaba Apagyi thisismydesign

View GitHub Profile
@thisismydesign
thisismydesign / main.dart
Created November 24, 2019 00:39
Dart TIL
void main() {
// Typed variables raise type errors
var typed1 = 1;
// typed1 = '2';
int typed2;
// typed2 = '2';
// Dynamic types don't raise type errors
var a;
a = 1;
@thisismydesign
thisismydesign / ruby_is_very_easy.rb
Created June 28, 2019 12:11
Debugging journal #1 - This will obviously not work. Right?.. /2
h.select(&:matcher)
# => {:a=>"this"}
# Sorry what?
@thisismydesign
thisismydesign / ruby_is_easy.rb
Last active June 28, 2019 12:10
Debugging journal #1 - This will obviously not work. Right?.. /1
# irb / rails c / whatever
h = { a: "this", b: "that" }
h.select{ |k, v| v.match?("this") }
# => {:a=>"this"}
def matcher(string)
string.match?("this")
end
@thisismydesign
thisismydesign / association_loading.rb
Last active June 12, 2019 12:12
Load first records of ordered association for a collection without N+1 queries
# https://github.com/rails/rails/issues/6769
# https://github.com/rails/rails/issues/10621
# https://stackoverflow.com/questions/19353507/eager-loading-the-first-record-of-an-association
# https://stackoverflow.com/questions/29142478/eager-load-only-first-associations-with-activerecord
# https://stackoverflow.com/questions/46415817/eager-loading-not-working-with-order-clause-in-rails
# https://stackoverflow.com/questions/52889750/eager-loading-with-scope-in-rails
# https://stackoverflow.com/questions/30056163/eager-loading-in-deep-level-nested-association
# https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Eager+loading+of+associations
# https://stackoverflow.com/questions/29804377/rails-4-eager-load-limit-subquery
# https://stackoverflow.com/questions/6477614/how-do-you-do-eager-loading-with-limits
@thisismydesign
thisismydesign / test.js
Last active September 6, 2023 12:46
Mocking per test case with Jest /1
jest.mock("MyOtherComponent", () => {
return require
.requireActual("TestUtils")
.mockOriginalFunctionality("MyOtherComponent");
});
describe("<Component />", () => {
it("works", () => {
// MyOtherComponent works as usual
}):
@thisismydesign
thisismydesign / TestUtils.js
Created February 10, 2019 02:49
Mocking per test case with Jest /2
export const mockOriginalFunctionality = name => {
const actualModule = require.requireActual(name);
return {
...Object.getOwnPropertyNames(actualModule)
.map(functionName => {
return {
[functionName]: jest.fn().mockImplementation(() => {
return actualModule[functionName]();
})
@thisismydesign
thisismydesign / test.js
Created February 10, 2019 02:27
Mocking per test case with Jest /1
describe("<Component />", () => {
it("works", () => {
// ...
}):
describe("when something silly happens", () => {
beforeEach(() => {
jest.mock("MyOtherComponent", () => {
// ...
})
@thisismydesign
thisismydesign / the_worst_is.js
Created February 1, 2019 20:57
JavaScript is not a language
// https://codeburst.io/javascript-null-vs-undefined-20f955215a2
// With default parameters, `null` does not use default parameters
let logDefault = (str = 'default') => {
console.log(str);
}
logHi();
// => hi
logHi(undefined);
// => hi
@thisismydesign
thisismydesign / test.rb
Last active January 26, 2019 18:03
Ruby: When and how to use factories for seeding? /3
# db/seeds/test.rb
build_stubbed(:user)
def seed_user_pagination_data
FactoryBot.create_list(:user, 10)
end
# user_spec.rb
it "returns user" do
# ...
@thisismydesign
thisismydesign / setup.rb
Last active January 26, 2019 17:54
Ruby: When and how to use factories for seeding? /2
# Gemfile
group :development, :test do
gem 'factory_bot_rails', require: false
end
# For testing:
# spec_helper.rb / rails_helper.rb
require 'factory_bot_rails'
# For other envs, e.g. development