How to use contextA method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

withContext.spec.js

Source:withContext.spec.js Github

copy

Full Screen

1import React, { Component, PureComponent } from 'react'2import PropTypes from 'prop-types'3import renderer from 'react-test-renderer'4import { createSelector, createStructuredSelector } from 'reselect'5import Enzyme, { mount } from 'enzyme'6import Adapter from 'enzyme-adapter-react-16'7import withContext from '../withContext'8Enzyme.configure({ adapter: new Adapter() })9function setup(propOverrides = {}) {10 const ref = React.createRef()11 const props = {12 ref,13 c: 3,14 ...propOverrides15 }16 class SomeComponent extends Component {17 static propTypes = {18 a: PropTypes.number.isRequired, // injected from ContextA19 b: PropTypes.number.isRequired, // injected from ContextB20 c: PropTypes.number.isRequired21 }22 render() {23 const { a, b, c } = this.props24 return [25 <div key="a" id="context-a">26 {a}27 </div>,28 <div key="b" id="context-b">29 {b}30 </div>,31 <div key="c" id="props-c">32 {c}33 </div>34 ]35 }36 }37 // The context will normally be exported elsewhere38 const ContextA = React.createContext()39 const ContextB = React.createContext()40 // this would normally look like41 // export default withContext(...)(SomeComponent)42 const Consumer = withContext([ContextA, ContextB], function mapContextToProps(43 context44 ) {45 return context46 })(SomeComponent)47 const tree = renderer.create(48 <ContextA.Provider value={{ a: 1 }}>49 <ContextB.Provider value={{ b: 2 }}>50 <div className="stuff">some other content</div>51 <div className="nested element">52 <Consumer {...props} />53 </div>54 </ContextB.Provider>55 </ContextA.Provider>56 )57 return {58 props,59 SomeComponent,60 ContextA,61 ContextB,62 Consumer,63 tree64 }65}66describe('ContextConsumerHOC', () => {67 it('matches snapshot', () => {68 const { tree } = setup()69 expect(tree.toJSON()).toMatchSnapshot()70 })71 it('should forward the references', () => {72 const { SomeComponent, props } = setup()73 const { ref } = props74 expect(ref.current.constructor).toEqual(SomeComponent)75 })76 it('should pass ownProps to mapContextToProps', () => {77 const props = { b: 2 }78 const childContextA = { a: 1 }79 const ContextA = React.createContext(childContextA)80 const Consumer = withContext([ContextA], (context, ownProps) => {81 expect(ownProps).toEqual(props)82 return {}83 })((props) => <div {...props} />)84 const tree = renderer.create(85 <ContextA.Provider value={childContextA}>86 <Consumer {...props} />87 </ContextA.Provider>88 )89 expect(tree.toJSON()).toMatchSnapshot()90 })91 it('should not re-render PureComponents if props did not change', () => {92 const spy = jest.fn()93 const ContextA = React.createContext()94 const ContextB = React.createContext()95 class NakedPureComponent extends PureComponent {96 render() {97 spy() // should run once98 return <div />99 }100 }101 const Consumer = withContext(102 [ContextA, ContextB],103 function mapContextToProps(context) {104 return context105 }106 )(NakedPureComponent)107 class App extends Component {108 static propTypes = {109 b: PropTypes.number110 }111 constructor(props) {112 super(props)113 this.state = {114 childContextB: { b: props.b }115 }116 // Keep reference to sabe context object to prevent re-renders117 this.childContextA = { a: 1 } // a doesn't change in this example118 }119 render() {120 return (121 <ContextA.Provider value={this.childContextA}>122 <ContextB.Provider value={this.state.childContextB}>123 <div className="stuff">some other content</div>124 <div className="nested element">125 <Consumer />126 </div>127 </ContextB.Provider>128 </ContextA.Provider>129 )130 }131 }132 const wrapper = mount(<App b={2} />)133 wrapper.setState({ random: 1 })134 // evidently, it is called 1 times135 expect(spy).toHaveBeenCalledTimes(1)136 wrapper.setState({137 childContextB: {138 b: 22139 }140 })141 // called 2 times because there is an update in the PureComponent' props142 expect(spy).toHaveBeenCalledTimes(2)143 })144 it('allows creating namespace selectors for individual context', () => {145 const contextA = { a: 1 }146 const contextB = { b: 2 }147 const ContextA = React.createContext()148 const ContextB = React.createContext()149 function NakedComponent(props) {150 return <div className="component" {...props} />151 }152 const App = withContext([ContextA, ContextB], function mapContextToProps({153 a,154 b155 }) {156 return {157 awrap: { a }, // WARNING: this creates a new object, use some type of memoization158 b159 }160 })(NakedComponent)161 const wrapper = mount(162 <ContextA.Provider value={contextA}>163 <ContextB.Provider value={contextB}>164 <App />165 </ContextB.Provider>166 </ContextA.Provider>167 )168 const component = wrapper.find('.component')169 expect(component.prop('awrap')).toEqual(contextA)170 expect(component.prop('b')).toEqual(2)171 })172 it('allows using memoization with reselect', () => {173 const spy = jest.fn()174 // selectors175 const getA = (context) => context.a176 // simple example177 const contextA = { a: 1, b: 2, c: 3 }178 const ContextA = React.createContext(contextA)179 const NakedComponent = (props) => {180 spy(props)181 return <div className="component" {...props} />182 }183 const App = withContext([ContextA], function mapContextToProps(context) {184 return {185 a: getA(context)186 }187 })(NakedComponent)188 const wrapper = mount(189 <ContextA.Provider value={contextA}>190 <App />191 </ContextA.Provider>192 )193 const component = wrapper.find('.component')194 expect(component.prop('a')).toEqual(contextA.a)195 expect(component.prop('b')).toBeUndefined()196 expect(component.prop('c')).toBeUndefined()197 expect(spy).toHaveBeenCalledWith({ a: 1 })198 })199 it('allows using memoization with reselect 2 (test using PureComponent)', () => {200 const spy = jest.fn()201 // selectors202 const getB = (context) => context.b203 const getC = (context) => context.c204 const getD = createSelector([getB, getC], (b, c) => b + c)205 // simple example206 const contextA = { a: 1, b: 2, c: 3 }207 const ContextA = React.createContext(contextA)208 // Because naked component doesn't care about prop 'c',209 // it only renders if 'a' or 'b' change, as expected.210 class NakedComponent extends PureComponent {211 render() {212 spy(this.props)213 return <div className="component" {...this.props} />214 }215 }216 const App = withContext([ContextA], function mapContextToProps(context) {217 return {218 d: getD(context)219 }220 })(NakedComponent)221 const Root = (props) => (222 <ContextA.Provider value={{ ...contextA, ...props }}>223 <App />224 </ContextA.Provider>225 )226 const wrapper = mount(<Root c={3} />)227 wrapper.setProps({ a: 2 })228 const component = wrapper.find('.component')229 expect(component.prop('a')).toBeUndefined()230 expect(component.prop('b')).toBeUndefined()231 expect(component.prop('c')).toBeUndefined()232 expect(component.prop('d')).toEqual(contextA.b + contextA.c)233 expect(spy).toHaveBeenCalledWith({ d: contextA.b + contextA.c })234 expect(spy).toHaveBeenCalledTimes(1)235 })236 it('allows using memoization with reselect 3 (namespaces with createStructuredSelector)', () => {237 const spy = jest.fn()238 // selectors239 const getB = (context) => context.b240 const getC = (context) => context.c241 const getD = createSelector([getB, getC], (b, c) => b + c)242 // simple example243 const contextA = { a: 1, b: 2, c: 3 }244 const ContextA = React.createContext(contextA)245 // Because naked component doesn't care about prop 'c',246 // it only renders if 'a' or 'b' change, as expected.247 class NakedComponent extends PureComponent {248 render() {249 spy(this.props)250 return <div className="component" {...this.props} />251 }252 }253 const App = withContext(254 [ContextA],255 createStructuredSelector({256 context: createStructuredSelector({257 d: getD258 })259 })260 )(NakedComponent)261 const Root = (props) => (262 <ContextA.Provider value={{ ...contextA, ...props }}>263 <App />264 </ContextA.Provider>265 )266 const wrapper = mount(<Root c={3} />)267 wrapper.setProps({ a: 2 })268 const component = wrapper.find('.component')269 expect(component.prop('context').a).toBeUndefined()270 expect(component.prop('context').b).toBeUndefined()271 expect(component.prop('context').c).toBeUndefined()272 expect(component.prop('context').d).toEqual(contextA.b + contextA.c)273 expect(spy).toHaveBeenCalledWith({274 context: { d: contextA.b + contextA.c }275 })276 expect(spy).toHaveBeenCalledTimes(1)277 })...

Full Screen

Full Screen

reducers.js

Source:reducers.js Github

copy

Full Screen

1/**2 * External dependencies3 */4import deepFreeze from 'deep-freeze';5/**6 * Internal dependencies7 */8import queryStateReducer from '../reducers';9import { setQueryValue, setValueForQueryContext } from '../actions';10describe( 'queryStateReducer', () => {11 const originalState = deepFreeze( {12 contexta: JSON.stringify( {13 foo: 'bar',14 cheese: 'pizza',15 } ),16 } );17 it(18 'returns original state when the action is not of the type being ' +19 'processed',20 () => {21 expect(22 queryStateReducer( originalState, { type: 'invalid' } )23 ).toBe( originalState );24 }25 );26 describe( 'SET_QUERY_KEY_VALUE action', () => {27 it(28 'returns original state when incoming query-state key value ' +29 'matches what is already in the state',30 () => {31 expect(32 queryStateReducer(33 originalState,34 setQueryValue( 'contexta', 'foo', 'bar' )35 )36 ).toBe( originalState );37 }38 );39 it(40 'returns new state when incoming query-state key exist ' +41 'but the value is a new value',42 () => {43 const newState = queryStateReducer(44 originalState,45 setQueryValue( 'contexta', 'foo', 'zed' )46 );47 expect( newState ).not.toBe( originalState );48 expect( newState ).toEqual( {49 contexta: JSON.stringify( {50 foo: 'zed',51 cheese: 'pizza',52 } ),53 } );54 }55 );56 it(57 'returns new state when incoming query-state key does not ' +58 'exist',59 () => {60 const newState = queryStateReducer(61 originalState,62 setQueryValue( 'contexta', 'burger', 'pizza' )63 );64 expect( newState ).not.toBe( originalState );65 expect( newState ).toEqual( {66 contexta: JSON.stringify( {67 foo: 'bar',68 cheese: 'pizza',69 burger: 'pizza',70 } ),71 } );72 }73 );74 } );75 describe( 'SET_QUERY_CONTEXT_VALUE action', () => {76 it(77 'returns original state when incoming context value matches ' +78 'what is already in the state',79 () => {80 expect(81 queryStateReducer(82 originalState,83 setValueForQueryContext( 'contexta', {84 foo: 'bar',85 cheese: 'pizza',86 } )87 )88 ).toBe( originalState );89 }90 );91 it(92 'returns new state when incoming context value is different ' +93 'than what is already in the state',94 () => {95 const newState = queryStateReducer(96 originalState,97 setValueForQueryContext( 'contexta', {98 bar: 'foo',99 pizza: 'cheese',100 } )101 );102 expect( newState ).not.toBe( originalState );103 expect( newState ).toEqual( {104 contexta: JSON.stringify( {105 bar: 'foo',106 pizza: 'cheese',107 } ),108 } );109 }110 );111 it(112 'returns new state when incoming context does not exist in the ' +113 'state',114 () => {115 const newState = queryStateReducer(116 originalState,117 setValueForQueryContext( 'contextb', {118 foo: 'bar',119 } )120 );121 expect( newState ).not.toBe( originalState );122 expect( newState ).toEqual( {123 contexta: JSON.stringify( {124 foo: 'bar',125 cheese: 'pizza',126 } ),127 contextb: JSON.stringify( {128 foo: 'bar',129 } ),130 } );131 }132 );133 } );...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const contextA = require('fast-check-monorepo/contextA');2contextA();3const contextB = require('fast-check-monorepo/contextB');4contextB();5const contextA = require('fast-check-monorepo/contextA');6contextA();7const contextB = require('fast-check-monorepo/contextB');8contextB();9const contextA = require('fast-check-monorepo/contextA');10contextA();11const contextB = require('fast-check-monorepo/contextB');12contextB();13const contextA = require('fast-check-monorepo/contextA');14contextA();15const contextB = require('fast-check-monorepo/contextB');16contextB();17const contextA = require('fast-check-monorepo/contextA');18contextA();19const contextB = require('fast-check-mon

Full Screen

Using AI Code Generation

copy

Full Screen

1import { contextA } from 'fast-check-monorepo';2contextA();3import { contextB } from 'fast-check-monorepo';4contextB();5import { contextA } from 'fast-check-monorepo';6contextA();7import { contextB } from 'fast-check-monorepo';8contextB();9 at Resolver.resolveModule (node_modules/jest-resolve/build/inde

Full Screen

Using AI Code Generation

copy

Full Screen

1import { contextA } from 'fast-check-monorepo';2import { contextB } from 'fast-check-monorepo';3import { contextC } from 'fast-check-monorepo';4import { contextD } from 'fast-check-monorepo';5import { contextE } from 'fast-check-monorepo';6import { contextF } from 'fast-check-monorepo';7import { contextG } from 'fast-check-monorepo';8import { contextH } from 'fast-check-monorepo';9import { contextI } from 'fast-check-monorepo';10import { contextJ } from 'fast-check-monorepo';11import { contextK } from 'fast-check-monorepo';12import { contextL } from 'fast-check-monorepo';13import { contextM } from 'fast-check-monorepo';14import { contextN } from 'fast-check-monorepo';15import { contextO } from 'fast-check-monorepo';16import { contextP } from 'fast-check-monorepo';17import { contextQ } from 'fast-check-monorepo';18import { contextR } from 'fast-check-monorepo';19import { contextS } from 'fast-check-monorepo

Full Screen

Using AI Code Generation

copy

Full Screen

1const contextA = require(‘fast-check-monorepo/contextA’);2contextA();3const contextB = require(‘fast-check-monorepo/contextB’);4contextB();5const contextA = require(‘fast-check-monorepo/contextA’);6contextA();7const contextB = require(‘fast-check-monorepo/contextB’);8contextB();

Full Screen

Using AI Code Generation

copy

Full Screen

1const { contextA } = require('@fast-check/contextA')2const { contextB } = require('@fast-check/contextB')3contextA()4contextB()5{6 "scripts": {7 },8 "dependencies": {9 }10}11const { contextA } = require('@fast-check/contextA')12const { contextB } = require('@fast-check/contextB')13const { beforeAll, afterAll, test } = require('jest-circus')14beforeAll(() => {15 const { currentTest } = require('jest-circus/build/state')16 currentTest.context = {17 }18})19afterAll(() => {20 const { currentTest } = require('jest-circus/build/state')21})22test('test', () => {23 const { currentTest } = require('jest-circus/build/state')24 currentTest.context.contextA()25 currentTest.context.contextB()26})

Full Screen

Using AI Code Generation

copy

Full Screen

1const contextA = require('fast-check-monorepo/contextA');2const contextB = require('fast-check-monorepo/contextB');3const contextC = require('fast-check-monorepo/contextC');4contextA.method();5contextB.method();6contextC.method();7const ContextA = require('./src/ContextA');8let instance = null;9module.exports = () => {10 if (instance === null) {11 instance = new ContextA();12 }13 return instance;14};15const ContextB = require('./src/ContextB');16let instance = null;

Full Screen

Using AI Code Generation

copy

Full Screen

1const contextA = require('fast-check-monorepo');2contextA.methodA();3module.exports = {4 methodA: () => {5 console.log('methodA');6 },7};8const contextA = require('fast-check-monorepo');9const a = new contextA.ClassA();10a.methodA();11class ClassA {12 methodA() {13 console.log('methodA');14 }15}16module.exports = {17};18const contextA = require('fast-check-monorepo');19const a = new contextA.ClassA();20a.methodA = () => {21 console.log('mocked methodA');22};23a.methodA();24class ClassA {25 methodA() {26 console.log('methodA');27 }28}29module.exports = {30};31const contextA = require('fast-check-monorepo');32const a = new contextA.ClassA();33a.methodB = () => {34 console.log('mocked methodB');35};36a.methodB();37class ClassA {38 methodB() {39 console.log('methodB');40 }41}42module.exports = {43};

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fast-check-monorepo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful