Git Product home page Git Product logo

fsf-test's Introduction

@bemedev/fsf-test

A test package for the @bemedev/fsf package


Install

npm install --save @bemedev/fsf-test //or
yarn add @bemedev/fsf-test //or
pnpm add @bemedev/fsf-test

Usage

import { createFunction } from '@bemedev/fsf';
import { test } from 'vitest';
import testFunction from './testFunction';

type Context = {
  apiKey?: string;
  apiUrl?: string;
  url?: string;
};

type Events = { products?: string[]; categories?: string[] };

const queryMachine = createFunction(
  {
    schema: {
      context: {} as Context,
      events: {} as Events,
      data: {} as string,
    },
    context: {},
    initial: 'preferences',
    states: {
      preferences: {
        always: {
          actions: ['setUrl', 'setApiKey', 'startUrl'],
          target: 'categories',
        },
      },
      categories: {
        always: [
          {
            cond: 'hasCategories',
            target: 'products',
            actions: 'setCategories',
          },
          'products',
        ],
      },
      products: {
        always: [
          {
            cond: 'hasProducts',
            target: 'final',
            actions: 'setProducts',
          },
          'final',
        ],
      },
      final: {
        data: 'query',
      },
    },
  },
  {
    // strict: true,
    actions: {
      setApiKey: ctx => {
        ctx.apiKey = '123';
      },
      setUrl: ctx => {
        ctx.apiUrl = 'https://example.com';
      },
      startUrl: ctx => {
        const { apiUrl, apiKey } = ctx;
        ctx.url = `${apiUrl}?apikey=${apiKey}`;
      },
      setCategories: (ctx, { categories }) => {
        const _categories = categories?.join(',');
        ctx.url += `&categories=${_categories}`;
      },
      setProducts: (ctx, { products }) => {
        const _products = products?.join(',');
        ctx.url += `&products=${_products}`;
      },
    },
    guards: {
      hasCategories: (_, { categories }) =>
        !!categories && categories.length > 0,
      hasProducts: (_, { products }) => !!products && products.length > 0,
    },
    datas: {
      query: ctx => ctx.url,
    },
  },
);

const testFunc = testFunction(queryMachine);

test('#1: no args', () => {
  testFunc().test(data => data === 'https://example.com?apikey=123');
});

test('#2: categories', () => {
  testFunc({ categories: ['a', 'b'] }).test(
    data => data === 'https://example.com?apikey=123&categories=a,b',
  );
});

test('#3: products', () => {
  testFunc({ products: ['a', 'b'] }).test(
    data => data === 'https://example.com?apikey=123&products=a,b',
  );
});

test('#4: categories and products', () => {
  testFunc({ products: ['a', 'b'], categories: ['c', 'd'] }).test(data => {
    return (
      data === 'https://example.com?apikey=123&categories=c,d&products=a,b'
    );
  });
});


😎         Enjoy your library !         😎

fsf-test's People

Contributors

chlbri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.