Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// - value must be one of:
// --> any unicode string
// --> null
// --> array containing values defined above (at least two items)
const queryParamsArbitrary = fastCheck.dictionary(
fastCheck.fullUnicodeString(1, 10),
fastCheck.oneof(
fastCheck.fullUnicodeString(),
fastCheck.constant(null),
fastCheck.array(fastCheck.oneof(fastCheck.fullUnicodeString(), fastCheck.constant(null)), 2, 10)
)
);
const optionsArbitrary = fastCheck.record({
arrayFormat: fastCheck.constantFrom('bracket', 'index', 'none'),
strict: fastCheck.boolean(),
encode: fastCheck.constant(true),
sort: fastCheck.constant(false)
}, {withDeletedKeys: true});
test('should read correctly from stringified query params', t => {
t.notThrows(() => {
fastCheck.assert(
fastCheck.property(
queryParamsArbitrary,
optionsArbitrary,
(object, options) => deepEqual(queryString.parse(queryString.stringify(object, options), options), object)
)
);
});
});
test('parse stringified object', () => {
const key = fc.fullUnicodeString()
const values = [
key,
fc.lorem(1000, false), // words
fc.lorem(100, true), // sentences
fc.boolean(),
fc.integer(),
fc.double(),
fc.constantFrom(null, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY)
]
const yamlArbitrary = fc.anything({ key: key, values: values })
const optionsArbitrary = fc.record(
{
keepBlobsInJSON: fc.boolean(),
keepCstNodes: fc.boolean(),
keepNodeTypes: fc.boolean(),
mapAsMap: fc.constant(false),
merge: fc.boolean(),
schema: fc.constantFrom('core', 'yaml-1.1') // ignore 'failsafe', 'json'
},
{ withDeletedKeys: true }
)
fc.assert(
fc.property(yamlArbitrary, optionsArbitrary, (obj, opts) => {
expect(YAML.parse(YAML.stringify(obj, opts), opts)).toStrictEqual(obj)
})
)
})
})
test('parse stringified object', () => {
const key = fc.fullUnicodeString()
const values = [
key,
fc.lorem(1000, false), // words
fc.lorem(100, true), // sentences
fc.boolean(),
fc.integer(),
fc.double(),
fc.constantFrom(null, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY)
]
const yamlArbitrary = fc.anything({ key: key, values: values })
const optionsArbitrary = fc.record(
{
keepBlobsInJSON: fc.boolean(),
keepCstNodes: fc.boolean(),
keepNodeTypes: fc.boolean(),
mapAsMap: fc.constant(false),
merge: fc.boolean(),
schema: fc.constantFrom('core', 'yaml-1.1') // ignore 'failsafe', 'json'
},
{ withDeletedKeys: true }
)
fc.assert(
fc.property(yamlArbitrary, optionsArbitrary, (obj, opts) => {
expect(YAML.parse(YAML.stringify(obj, opts), opts)).toStrictEqual(obj)
})
)
})
})
it('should detect potential issues with the TodoList', async () =>
await fc.assert(
fc
.asyncProperty(
fc.scheduler({ act }),
TodoListCommands,
fc.set(
fc.record({ id: fc.hexaString(8, 8), label: fc.string(), checked: fc.boolean() }),
(a, b) => a.id === b.id
),
fc.infiniteStream(fc.boolean()),
async (s, commands, initialTodos, allFailures) => {
const { mockedApi, expectedTodos } = mockApi(s, initialTodos, allFailures);
// Execute all the commands
const wrapper = render();
await fc.scheduledModelRun(s, () => ({ model: { todos: [], wrapper }, real: {} }), commands);
// Check the final state (no more items should be loading)
expect(
sortTodos(
(await listTodos(wrapper)).map(t => ({ label: t.label, checked: t.checked, loading: t.loading }))
)
).toEqual(sortTodos(expectedTodos().map(t => ({ label: t.label, checked: t.checked, loading: false }))));
it('should detect potential issues with the TodoList', async () =>
await fc.assert(
fc
.asyncProperty(
fc.scheduler({ act }),
TodoListCommands,
fc.set(
fc.record({ id: fc.hexaString(8, 8), label: fc.string(), checked: fc.boolean() }),
(a, b) => a.id === b.id
),
fc.infiniteStream(fc.boolean()),
async (s, commands, initialTodos, allFailures) => {
const { mockedApi, expectedTodos } = mockApi(s, initialTodos, allFailures);
// Execute all the commands
const wrapper = render();
await fc.scheduledModelRun(s, () => ({ model: { todos: [], wrapper }, real: {} }), commands);
// Check the final state (no more items should be loading)
expect(
sortTodos(
(await listTodos(wrapper)).map(t => ({ label: t.label, checked: t.checked, loading: t.loading }))
)
).toEqual(sortTodos(expectedTodos().map(t => ({ label: t.label, checked: t.checked, loading: false }))));
}
)
.beforeEach(async () => {
it('successful unary call', async () => {
await fc.assert(
fc.asyncProperty(
fc.record({
method: lowerCamelCase(),
requestContext: context(),
responseContext: context(),
useCompression: fc.boolean(),
request: fc.dictionary(fc.string(), fc.jsonObject()),
useRequestFunction: fc.boolean(),
}),
async arb => {
const app = express();
const handler: ModuleRpcServer.ServiceHandlerFor<
typeof unaryServiceDefinition,
any
> = {
async unary(request, requestContext) {
return {
fooRequest: request,
barRequestContext: requestContext,
};
},
};
app.use(
'/api',
it('successful server-stream call', async () => {
await fc.assert(
fc.asyncProperty(
fc.record({
method: lowerCamelCase(),
requestContext: context(),
responseContext: context(),
useCompression: fc.boolean(),
request: fc.array(fc.dictionary(fc.string(), fc.jsonObject())),
}),
async arb => {
const app = express();
const handler: ModuleRpcServer.ServiceHandlerFor<
typeof serverStreamServiceDefinition,
any
> = {
async serverStream(
request: any[],
{ onReady, onMessage },
requestContext,
) {
onReady(() => {});
for (const item of request) {
onMessage({
it('getSerializedPropertyType', () => {
assert(
property(string(), serializedTypeArbitrary, boolean(), (name, s, isRequired) => {
const serialized = getSerializedOptionPropertyType(name, isRequired)(s);
const safeName = UNSAFE_PROPERTY_PATTERN.test(name) ? `['${name}']` : name;
const expected = isRequired
? serializedType(`${safeName}: ${s.type}`, `${safeName}: ${s.io}`, s.dependencies, s.refs)
: serializedType(
`${safeName}: Option<${s.type}>`,
`${safeName}: optionFromNullable(${s.io})`,
[
...s.dependencies,
serializedDependency('Option', 'fp-ts/lib/Option'),
serializedDependency('optionFromNullable', 'io-ts-types/lib/optionFromNullable'),
],
s.refs,
);
expect(serialized).toEqual(expected);
}),
export function arbEitherIO(arbe: Arbitrary, arba: Arbitrary<a>): Arbitrary> {
return fc.boolean()
.chain((error) => error ? arbErrorIO(arbe) : arbIO(arba));
}
</a>
export const fullBinaryTree = (maxDepth: number): fc.Arbitrary> => {
const valueArbitrary = fc.integer();
if (maxDepth <= 0) {
return fc.record({
value: valueArbitrary,
left: fc.constant(null),
right: fc.constant(null)
});
}
const subTree = fullBinaryTree(maxDepth - 1);
return fc.boolean().chain(
(hasChildren: boolean): fc.Arbitrary> => {
if (hasChildren) {
return fc.record({
value: valueArbitrary,
left: subTree,
right: subTree
});
}
return fc.record({
value: valueArbitrary,
left: fc.constant(null),
right: fc.constant(null)
});
}
);
};