Skip to content

Instantly share code, notes, and snippets.

View thomashagstrom's full-sized avatar

Thomas Hagström thomashagstrom

  • Chas Consulting AB
  • Stockholm, Sweden
View GitHub Profile
@thomashagstrom
thomashagstrom / GetJwtToken.test.ts
Last active October 23, 2023 14:52
Mock for single test
import { Auth } from 'aws-amplify';
import GetJwtToken from './GetJwtToken';
describe('jwt', () => {
describe('GetJwtToken', () => {
it('Original mock for Auth class', async () => {
const result: Error = (await GetJwtToken()) as Error;
expect(result.message).toBe('Could not get session');
});
@thomashagstrom
thomashagstrom / AsyncJest.test.tsx
Last active January 15, 2019 16:57
Call async function in Jest test
it('Error label should contain `No userPool`', async () => {
const wrapper = TestRenderer.create(<CognitoLogin />);
const child = wrapper.root.findByProps({ iconName: 'sign-in' });
await child.props.onPress();
const errorLabel = wrapper.root.findByProps({ isCaption: true });
expect(errorLabel.props.children).toBe('No userPool');
});
@thomashagstrom
thomashagstrom / CrossButton.tsx
Last active January 15, 2019 16:15
CrossButton with child FontAwesome button
export class CrossButton extends React.Component<ICrossButtonProps> {
render() {
return (
<View style={this.props.style ? this.props.style : styles.container}>
{_.isNil(this.props.title) && !_.isNil(this.props.iconName) ? (
<FontAwesome.Button
testID="FontAwesomeButton"
onPress={() => {
if (this.props.onPress) {
this.props.onPress();
@thomashagstrom
thomashagstrom / CrossButton.test.tsx
Last active January 15, 2019 15:56
Callback on React child using Jest
import React from 'react';
import CrossButton from './CrossButton';
import TestRenderer from 'react-test-renderer';
describe('components', () => {
describe('<CrossButton />', () => {
it('<FontAwesome.Button /> onPress should be called', () => {
let called = false;
const onCalled = () => (called = !called);
@thomashagstrom
thomashagstrom / CompFunc.test.tsx
Created January 14, 2019 07:55
Test React component function
it('`onEmailChanged` should update state', () => {
const wrapper = TestRenderer.create(<CognitoLogin />);
const expectedEmail = 'bogus@stuff.com';
wrapper.root.instance.onEmailChanged(expectedEmail);
expect(wrapper.root.instance.state.userInput.email).toBe(expectedEmail);
});
@thomashagstrom
thomashagstrom / CompArrowFunc.tsx
Created January 14, 2019 07:51
React component with arrow function
export class CognitoLogin extends React.Component<
ICognitoLoginProps,
ICognitoLoginState
> {
constructor(props: ICognitoLoginProps) {
super(props);
this.state = {
userInput: __DEV__
? {
email: 'cogtest@mailinator.com',
@thomashagstrom
thomashagstrom / JestReadReactState.test.tsx
Created January 13, 2019 12:30
Read React state in Jest test
import React from 'react';
import TestRenderer from 'react-test-renderer';
import CognitoLogin from './CognitoLogin';
import _ from 'lodash';
it('When no `code` or `email` `onSavePress` should set `message` to `Need code and user e-mail`', async (done) => {
// Arrange
const wrapper = TestRenderer.create(<CognitoLogin />);
const child = wrapper.root.findByProps({
testID: 'ForgotForm',
@thomashagstrom
thomashagstrom / FindByTestID.test.tsx
Last active January 13, 2019 12:22
Jest find React child by testID
it('On forgot password should not throw', () => {
const wrapper = TestRenderer.create(<CognitoLogin />);
const child = wrapper.root.findByProps({
testID: 'ForgotForm',
});
expect(child).toBeDefined();
});
@thomashagstrom
thomashagstrom / JestSetState.tsx
Created January 13, 2019 12:10
Jest set React component state
const wrapper = TestRenderer.create(<CognitoLogin />);
const state: ICognitoLoginState = {
formState: 'Register',
code: 'testcode',
userInput: { email: 'bogus@test.com', password: 'testPw' },
result: undefined,
};
wrapper.root.instance.setState(state);
@thomashagstrom
thomashagstrom / Codecov.sh
Created January 11, 2019 22:58
Upload to codecov
echo Upload code coverage
echo Token: "$(CODECOV_TOKEN)"
codecov -t "$(CODECOV_TOKEN)"