Best JavaScript code snippet using fast-check-monorepo
ReactEmptyComponent-test.js
Source:ReactEmptyComponent-test.js
1/**2 * Copyright (c) Facebook, Inc. and its affiliates.3 *4 * This source code is licensed under the MIT license found in the5 * LICENSE file in the root directory of this source tree.6 *7 * @emails react-core8 */9'use strict';10let React;11let ReactDOM;12let ReactTestUtils;13let TogglingComponent;14let log;15describe('ReactEmptyComponent', () => {16 beforeEach(() => {17 jest.resetModules();18 React = require('react');19 ReactDOM = require('react-dom');20 ReactTestUtils = require('react-dom/test-utils');21 log = jest.fn();22 TogglingComponent = class extends React.Component {23 state = {component: this.props.firstComponent};24 componentDidMount() {25 log(ReactDOM.findDOMNode(this));26 this.setState({component: this.props.secondComponent});27 }28 componentDidUpdate() {29 log(ReactDOM.findDOMNode(this));30 }31 render() {32 const Component = this.state.component;33 return Component ? <Component /> : null;34 }35 };36 });37 describe.each([null, undefined])('when %s', nullORUndefined => {38 it('should not throw when rendering', () => {39 class Component extends React.Component {40 render() {41 return nullORUndefined;42 }43 }44 expect(function() {45 ReactTestUtils.renderIntoDocument(<Component />);46 }).not.toThrowError();47 });48 it('should not produce child DOM nodes for nullish and false', () => {49 class Component1 extends React.Component {50 render() {51 return nullORUndefined;52 }53 }54 class Component2 extends React.Component {55 render() {56 return false;57 }58 }59 const container1 = document.createElement('div');60 ReactDOM.render(<Component1 />, container1);61 expect(container1.children.length).toBe(0);62 const container2 = document.createElement('div');63 ReactDOM.render(<Component2 />, container2);64 expect(container2.children.length).toBe(0);65 });66 it('should be able to switch between rendering nullish and a normal tag', () => {67 const instance1 = (68 <TogglingComponent69 firstComponent={nullORUndefined}70 secondComponent={'div'}71 />72 );73 const instance2 = (74 <TogglingComponent75 firstComponent={'div'}76 secondComponent={nullORUndefined}77 />78 );79 ReactTestUtils.renderIntoDocument(instance1);80 ReactTestUtils.renderIntoDocument(instance2);81 expect(log).toHaveBeenCalledTimes(4);82 expect(log).toHaveBeenNthCalledWith(1, null);83 expect(log).toHaveBeenNthCalledWith(84 2,85 expect.objectContaining({tagName: 'DIV'}),86 );87 expect(log).toHaveBeenNthCalledWith(88 3,89 expect.objectContaining({tagName: 'DIV'}),90 );91 expect(log).toHaveBeenNthCalledWith(4, null);92 });93 it('should be able to switch in a list of children', () => {94 const instance1 = (95 <TogglingComponent96 firstComponent={nullORUndefined}97 secondComponent={'div'}98 />99 );100 ReactTestUtils.renderIntoDocument(101 <div>102 {instance1}103 {instance1}104 {instance1}105 </div>,106 );107 expect(log).toHaveBeenCalledTimes(6);108 expect(log).toHaveBeenNthCalledWith(1, null);109 expect(log).toHaveBeenNthCalledWith(2, null);110 expect(log).toHaveBeenNthCalledWith(3, null);111 expect(log).toHaveBeenNthCalledWith(112 4,113 expect.objectContaining({tagName: 'DIV'}),114 );115 expect(log).toHaveBeenNthCalledWith(116 5,117 expect.objectContaining({tagName: 'DIV'}),118 );119 expect(log).toHaveBeenNthCalledWith(120 6,121 expect.objectContaining({tagName: 'DIV'}),122 );123 });124 it('should distinguish between a script placeholder and an actual script tag', () => {125 const instance1 = (126 <TogglingComponent127 firstComponent={nullORUndefined}128 secondComponent={'script'}129 />130 );131 const instance2 = (132 <TogglingComponent133 firstComponent={'script'}134 secondComponent={nullORUndefined}135 />136 );137 expect(function() {138 ReactTestUtils.renderIntoDocument(instance1);139 }).not.toThrow();140 expect(function() {141 ReactTestUtils.renderIntoDocument(instance2);142 }).not.toThrow();143 expect(log).toHaveBeenCalledTimes(4);144 expect(log).toHaveBeenNthCalledWith(1, null);145 expect(log).toHaveBeenNthCalledWith(146 2,147 expect.objectContaining({tagName: 'SCRIPT'}),148 );149 expect(log).toHaveBeenNthCalledWith(150 3,151 expect.objectContaining({tagName: 'SCRIPT'}),152 );153 expect(log).toHaveBeenNthCalledWith(4, null);154 });155 it(156 'should have findDOMNode return null when multiple layers of composite ' +157 'components render to the same nullish placeholder',158 () => {159 class GrandChild extends React.Component {160 render() {161 return nullORUndefined;162 }163 }164 class Child extends React.Component {165 render() {166 return <GrandChild />;167 }168 }169 const instance1 = (170 <TogglingComponent firstComponent={'div'} secondComponent={Child} />171 );172 const instance2 = (173 <TogglingComponent firstComponent={Child} secondComponent={'div'} />174 );175 expect(function() {176 ReactTestUtils.renderIntoDocument(instance1);177 }).not.toThrow();178 expect(function() {179 ReactTestUtils.renderIntoDocument(instance2);180 }).not.toThrow();181 expect(log).toHaveBeenCalledTimes(4);182 expect(log).toHaveBeenNthCalledWith(183 1,184 expect.objectContaining({tagName: 'DIV'}),185 );186 expect(log).toHaveBeenNthCalledWith(2, null);187 expect(log).toHaveBeenNthCalledWith(3, null);188 expect(log).toHaveBeenNthCalledWith(189 4,190 expect.objectContaining({tagName: 'DIV'}),191 );192 },193 );194 it('works when switching components', () => {195 let assertions = 0;196 class Inner extends React.Component {197 render() {198 return <span />;199 }200 componentDidMount() {201 // Make sure the DOM node resolves properly even if we're replacing a202 // `null` component203 expect(ReactDOM.findDOMNode(this)).not.toBe(null);204 assertions++;205 }206 componentWillUnmount() {207 // Even though we're getting replaced by `null`, we haven't been208 // replaced yet!209 expect(ReactDOM.findDOMNode(this)).not.toBe(null);210 assertions++;211 }212 }213 class Wrapper extends React.Component {214 render() {215 return this.props.showInner ? <Inner /> : nullORUndefined;216 }217 }218 const el = document.createElement('div');219 let component;220 // Render the <Inner /> component...221 component = ReactDOM.render(<Wrapper showInner={true} />, el);222 expect(ReactDOM.findDOMNode(component)).not.toBe(null);223 // Switch to null...224 component = ReactDOM.render(<Wrapper showInner={false} />, el);225 expect(ReactDOM.findDOMNode(component)).toBe(null);226 // ...then switch back.227 component = ReactDOM.render(<Wrapper showInner={true} />, el);228 expect(ReactDOM.findDOMNode(component)).not.toBe(null);229 expect(assertions).toBe(3);230 });231 it('can render nullish at the top level', () => {232 const div = document.createElement('div');233 ReactDOM.render(nullORUndefined, div);234 expect(div.innerHTML).toBe('');235 });236 it('does not break when updating during mount', () => {237 class Child extends React.Component {238 componentDidMount() {239 if (this.props.onMount) {240 this.props.onMount();241 }242 }243 render() {244 if (!this.props.visible) {245 return nullORUndefined;246 }247 return <div>hello world</div>;248 }249 }250 class Parent extends React.Component {251 update = () => {252 this.forceUpdate();253 };254 render() {255 return (256 <div>257 <Child key="1" visible={false} />258 <Child key="0" visible={true} onMount={this.update} />259 <Child key="2" visible={false} />260 </div>261 );262 }263 }264 expect(function() {265 ReactTestUtils.renderIntoDocument(<Parent />);266 }).not.toThrow();267 });268 it('preserves the dom node during updates', () => {269 class Empty extends React.Component {270 render() {271 return nullORUndefined;272 }273 }274 const container = document.createElement('div');275 ReactDOM.render(<Empty />, container);276 const noscript1 = container.firstChild;277 expect(noscript1).toBe(null);278 // This update shouldn't create a DOM node279 ReactDOM.render(<Empty />, container);280 const noscript2 = container.firstChild;281 expect(noscript2).toBe(null);282 });283 it('should not warn about React.forwardRef that returns nullish', () => {284 const Empty = () => {285 return nullORUndefined;286 };287 const EmptyForwardRef = React.forwardRef(Empty);288 expect(() => {289 ReactTestUtils.renderIntoDocument(<EmptyForwardRef />);290 }).not.toThrowError();291 });292 it('should not warn about React.memo that returns nullish', () => {293 const Empty = () => {294 return nullORUndefined;295 };296 const EmptyMemo = React.memo(Empty);297 expect(() => {298 ReactTestUtils.renderIntoDocument(<EmptyMemo />);299 }).not.toThrowError();300 });301 });...
Using AI Code Generation
1const fc = require("fast-check");2const { orUndefined } = require("fast-check-monorepo");3fc.assert(4 fc.property(orUndefined(fc.integer()), (i) => {5 return i === undefined || Number.isInteger(i);6 })7);8const fc = require("fast-check");9const { orUndefined } = require("fast-check-monorepo");10fc.assert(11 fc.property(orUndefined(fc.integer()), (i) => {12 return i === undefined || Number.isInteger(i);13 })14);15const fc = require("fast-check");16const { orUndefined } = require("fast-check-monorepo");17fc.assert(18 fc.property(orUndefined(fc.integer()), (i) => {19 return i === undefined || Number.isInteger(i);20 })21);22const fc = require("fast-check");23const { orUndefined } = require("fast-check-monorepo");24fc.assert(25 fc.property(orUndefined(fc.integer()), (i) => {26 return i === undefined || Number.isInteger(i);27 })28);29const fc = require("fast-check");30const { orUndefined } = require("fast-check-monorepo");31fc.assert(32 fc.property(orUndefined(fc.integer()), (i) => {33 return i === undefined || Number.isInteger(i);34 })35);36const fc = require("fast-check");37const { orUndefined } = require("fast-check-monorepo");38fc.assert(39 fc.property(orUndefined(fc.integer()), (i) => {40 return i === undefined || Number.isInteger(i);41 })42);43const fc = require("fast-check");44const { orUndefined } = require("fast-check-monorepo");45fc.assert(46 fc.property(orUndefined(fc.integer()), (i) => {47 return i === undefined || Number.isInteger(i);48 })49);
Using AI Code Generation
1const fc = require('fast-check');2const { orUndefined } = require('fast-check/lib/types/arbitrary/definition/ArbitraryWithShrink');3const { array } = require('fast-check/lib/types/arbitrary/ArrayArbitrary');4const { constantFrom } = require('fast-check/lib/types/arbitrary/ConstantArbitrary');5const { integer } = require('fast-check/lib/types/arbitrary/IntegerArbitrary');6const arr = array(constantFrom(1, 2, 3, 4, 5));7const arb = orUndefined(arr);8fc.assert(9 fc.property(arb, (x) => {10 if (x === null) {11 return true;12 }13 return arr.canShrinkWithoutContext(x);14 })15);16const fc = require('fast-check');17const { orUndefined } = require('fast-check/lib/types/arbitrary/definition/ArbitraryWithShrink');18const { array } = require('fast-check/lib/types/arbitrary/ArrayArbitrary');19const { constantFrom } = require('fast-check/lib/types/arbitrary/ConstantArbitrary');20const { integer } = require('fast-check/lib/types/arbitrary/IntegerArbitrary');21const arr = array(constantFrom(1, 2, 3, 4, 5));22const arb = orUndefined(arr);23fc.assert(24 fc.property(arb, (x) => {25 if (x === null) {26 return true;27 }28 return arr.canShrinkWithoutContext(x);29 })30);31const fc = require('fast-check');32const { orUndefined } = require('fast-check/lib/types/arbitrary/definition/ArbitraryWithShrink');33const { array } = require('fast-check/lib/types/arbitrary/ArrayArbitrary');34const { constantFrom } = require('fast-check/lib/types/arbitrary/ConstantArbitrary');35const { integer } = require('fast-check/lib/types/arbitrary/IntegerArbitrary');36const arr = array(constantFrom(1, 2, 3, 4, 5));37const arb = orUndefined(arr);38fc.assert(39 fc.property(arb, (x) => {40 if (x === null) {41 return true;42 }
Using AI Code Generation
1const {check, property} = require('fast-check');2const {orUndefined} = require('fast-check/lib/check/arbitrary/definition/ArbitraryWithShrink');3const arb = orUndefined(property.arb(property.integer()));4check(arb, {examples: [[undefined]]});5const {check, property} = require('fast-check');6const {orUndefined} = require('fast-check/lib/check/arbitrary/definition/ArbitraryWithShrink');7const arb = orUndefined(property.arb(property.integer()));8check(arb, {examples: [[undefined]]});9const {check, property} = require('fast-check');10const {orUndefined} = require('fast-check/lib/check/arbitrary/definition/ArbitraryWithShrink');11const arb = orUndefined(property.arb(property.integer()));12check(arb, {examples: [[undefined]]});13const {check, property} = require('fast-check');14const {orUndefined} = require('fast-check/lib/check/arbitrary/definition/ArbitraryWithShrink');15const arb = orUndefined(property.arb(property.integer()));16check(arb, {examples: [[undefined]]});17const {check, property} = require('fast-check');18const {orUndefined} = require('fast-check/lib/check/arbitrary/definition/ArbitraryWithShrink');19const arb = orUndefined(property.arb(property.integer()));20check(arb, {examples: [[undefined]]});21const {check, property} = require('fast-check');
Using AI Code Generation
1const {ArbitraryExtensions} = require('fast-check-monorepo/packages/arbitrary-extensions/dist/ArbitraryExtensions');2const {fc} = require('fast-check-monorepo/packages/fast-check/dist/index');3const arb = fc.string().orUndefined();4const arb = ArbitraryExtensions.orUndefined(fc.string());5const arb = ArbitraryExtensions.orUndefined(fc.string(), 'myUndefinedValue');6const arb = ArbitraryExtensions.orUndefined(fc.string(), () => 'myUndefinedValue');7const arb = ArbitraryExtensions.orUndefined(fc.string(), () => fc.oneof(fc.constant('myUndefinedValue'), fc.constant('myOtherUndefinedValue')));8const arb = ArbitraryExtensions.orUndefined(fc.string(), () => fc.oneof(fc.constant('myUndefinedValue'), fc.constant('myOtherUndefinedValue')), 'myUndefinedValue');9const arb = ArbitraryExtensions.orUndefined(fc.string(), () => fc.oneof(fc.constant('myUndefinedValue'), fc.constant('myOtherUndefinedValue')), () => 'myUndefinedValue');10const arb = ArbitraryExtensions.orUndefined(fc.string(), () => fc.oneof(fc.constant('myUndefinedValue'), fc.constant('myOtherUndefinedValue')), () => fc.oneof(fc.constant('myUndefinedValue'), fc.constant('myOtherUndefinedValue')));11const arb = ArbitraryExtensions.orUndefined(fc.string(), () => fc.oneof(fc.constant('myUndefinedValue'), fc.constant('myOtherUndefinedValue')), () => fc.oneof(fc.constant('myUndefinedValue'), fc.constant('myOtherUndefinedValue')), 'myUndefinedValue');12const arb = ArbitraryExtensions.orUndefined(fc.string(), () => fc.oneof(fc.constant('myUndefinedValue'), fc.constant('myOtherUndefinedValue')), () => fc.oneof(fc.constant('myUndefinedValue'), fc.constant('myOtherUndefinedValue')), () => 'myUndefinedValue');
Using AI Code Generation
1const fc = require('fast-check');2const { orUndefined } = require('fast-check-monorepo');3const prop = fc.property(orUndefined(fc.integer()), (value) => {4 return value === undefined || Number.isInteger(value);5});6fc.assert(prop, { numRuns: 1000 });7const fc = require('fast-check');8const { orUndefined } = require('fast-check-monorepo');9const prop = fc.property(orUndefined(fc.integer()), (value) => {10 return value === undefined || Number.isInteger(value);11});12fc.assert(prop, { numRuns: 1000 });13const fc = require('fast-check');14const { orUndefined } = require('fast-check-monorepo');15const prop = fc.property(orUndefined(fc.integer()), (value) => {16 return value === undefined || Number.isInteger(value);17});18fc.assert(prop, { numRuns: 1000 });19const fc = require('fast-check');20const { orUndefined } = require('fast-check-monorepo');21const prop = fc.property(orUndefined(fc.integer()), (value) => {22 return value === undefined || Number.isInteger(value);23});24fc.assert(prop, { numRuns: 1000 });25const fc = require('fast-check');26const { orUndefined } = require('fast-check-monorepo');27const prop = fc.property(orUndefined(fc.integer()), (value) => {28 return value === undefined || Number.isInteger(value);29});30fc.assert(prop, { numRuns: 1000 });31const fc = require('fast-check');32const { orUndefined } = require('fast-check-monorepo');33const prop = fc.property(orUndefined(fc.integer()), (value) => {34 return value === undefined || Number.isInteger(value);35});36fc.assert(prop, { numRuns: 1000 });37const fc = require('fast
Using AI Code Generation
1const fc = require("fast-check");2const { orUndefined } = fc;3const arb = orUndefined(fc.integer());4fc.assert(fc.property(arb, (v) => v === undefined || typeof v === "number"));5const fc = require("fast-check");6const { orUndefined } = fc;7const arb = orUndefined(fc.integer());8fc.assert(fc.property(arb, (v) => v === undefined || typeof v === "number"));9const fc = require("fast-check");10const { orUndefined } = fc;11const arb = orUndefined(fc.integer());12fc.assert(fc.property(arb, (v) => v === undefined || typeof v === "number"));13const fc = require("fast-check");14const { orUndefined } = fc;15const arb = orUndefined(fc.integer());16fc.assert(fc.property(arb, (v) => v === undefined || typeof v === "number"));17const fc = require("fast-check");18const { orUndefined } = fc;19const arb = orUndefined(fc.integer());20fc.assert(fc.property(arb, (v) => v === undefined || typeof v === "number"));21const fc = require("fast-check");22const { orUndefined } = fc;23const arb = orUndefined(fc.integer());24fc.assert(fc.property(arb, (v) => v === undefined || typeof v === "number"));25const fc = require("fast-check");26const { orUndefined } = fc;27const arb = orUndefined(fc.integer());28fc.assert(fc.property(arb, (v) => v === undefined || typeof v === "number"));29const fc = require("fast-check");30const { orUndefined } = fc;31const arb = orUndefined(fc.integer());32fc.assert(fc.property(arb, (v) => v
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!