# chai-extra-functionblock

> Testing function block code

Latest version **1.0.12** (published 2023-05-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install chai-extra-functionblock
pnpm add chai-extra-functionblock
yarn add chai-extra-functionblock
bun add chai-extra-functionblock
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.12 |
| Published | 2023-05-30 |
| First published | 2023-05-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 19.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Mayank P Patel |
| Maintainers | darthvader202 |
| Keywords | chai extended, function syntax, chai, helper methods, helper properties |

## Links

- npm: https://www.npmjs.com/package/chai-extra-functionblock
- npm.io page: https://npm.io/package/chai-extra-functionblock

## Dependencies (1)

- [esprima](https://npm.io/package/esprima.md) ^4.0.1

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.0.12 (latest) — 2023-05-30
- 1.0.11 — 2023-05-30
- 1.0.10 — 2023-05-24
- 1.0.9 — 2023-05-24
- 1.0.8 — 2023-05-22
- 1.0.7 — 2023-05-19
- 1.0.6 — 2023-05-18
- 1.0.5 — 2023-05-13
- 1.0.4 — 2023-05-09
- 1.0.3 — 2023-05-05
- 1.0.2 — 2023-05-04
- 1.0.1 — 2023-05-04
- 1.0.0 — 2023-05-04

## README

# How to use this library
1. run `npm install chai-extra-functionblock`
2. In your test file - 
  - `import 'chai-extra-functionblock'`
4. write your test using the added custom methods and properties to chai


# Chai `Arrow Function` Assertion
The `arrowFunction` property allows you to check if a given function is an Arrow Function.

## Usage:

```javascript
const myArrowFunction = () => {
  return 'Hello, world!';
};

const myFunctionExpression = function() {
  return 'Hello, world!';
};

expect(myArrowFunction).to.be.an.arrowFunction;
expect(myFunctionExpression).to.not.be.arrowFunction;
```

# Chai `Function Expression` Assertion
The `functionExpression` property allows you to check if a given function is a Function Expression.

## Usage:

```javascript
const myFunctionExpression = function() {
  return 'Hello, world!';
};

const myArrowFunction = () => {
  return 'Hello, world!';
};

expect(myFunctionExpression).to.be.a.functionExpression;
expect(myArrowFunction).to.not.be.functionExpression;
```

# Chai `Function Declaration` Assertion
The `functionDeclaration` property allows you to check if a given function is a Function Declaration.

## Usage:

```javascript
function myFunctionDeclaration() {
  return 'Hello, world!';
}

const myFunctionExpression = function() {
  return 'Hello, world!';
};

expect(myFunctionDeclaration).to.be.a.functionDeclaration;
expect(myFunctionExpression).to.not.be.functionDeclaration;
```

# Chai `RegularForLoop` Property

The `regularForLoop` property allows you to check if a given function uses a regular for loop.

## Usage:
```javascript

const fn = function() {
  for (let i = 0; i < 10; i++) {
    // Loop body
  }
}  
expect(fn).to.have.regularForLoop;

const fn = function() {
  while (true) {
    // Loop body
  }
};

expect(fn).to.not.have.regularForLoop;
```

# Chai `ForOfLoop` Property

The `forOfLoop` property allows you to check if a given function uses a `for of` loop.

## Usage:
```javascript

const fn = function(arr) {
  for (var ele of arr) {
    // Loop body
  }
}  
expect(fn).to.have.forOfLoop;

const fn = function() {
  while (true) {
    // Loop body
  }
};

expect(fn).to.not.have.forOfLoop;
```

# Chai `ForInLoop` Property

The `forInLoop` property allows you to check if a given function uses a `for in` loop.

## Usage:
```javascript

const fn = function(obj) {
  for (var key in obj) {
    // Loop body
  }
}  
expect(fn).to.have.forInLoop;

const fn = function() {
  while (true) {
    // Loop body
  }
};

expect(fn).to.not.have.forInLoop;
```

# Chai `WhileLoop` Property

The `whileLoop` property allows you to check if a given function uses a `while` loop.

## Usage:
```javascript

const fn = function() {
  while (true) {
   // Loop body
  }
  
}  
expect(fn).to.have.whileLoop;

const fn = function() {
  for (var key in obj) {
    // Loop body
  }
};

expect(fn).to.not.have.whileLoop;
```

# Chai `Operator` Method

The `operator` property allows you to check if a given function uses a any `operators` in you function code.

**Note**
`=`is not considered an operator when used in `Variable Declaration`.

## Usage:
```javascript

const fn = function() {
  let arr = ['a', 'b','c', 'd'];
  let index = 0;
  for(var i = 0; i < arr.length; i++) {
    if(arr[i] === 'c' && typeof arr[i] === 'string') {
      index = i;
    }
  }
  return index
}  

expect(fn).to.have.operator("<");
expect(fn).to.not.have.operator(">");
expect(fn).to.have.operator("===");
expect(fn).to.have.operator("=");
expect(fn).to.have.operator("typeof");
```

# Chai `keyword` Method

The `keyword` property allows you to check if a given function uses any `keyword` in your function code.


## Usage:
```javascript

const fn = function() {
  let arr = ['a', 'b','c', 'd'];
  let index = 0;
  for(let i = 0; i < arr.length; i++) {
    if(arr[i] === 'c' && typeof arr[i] === 'string') {
      index = i;
    }
  }
  return index
}  

expect(fn).to.have.keyword("let");
expect(fn).to.not.have.keyword("var");
expect(fn).to.have.keyword("if");

```

# Chai `throwJavaScriptError` Method

The `throwJavaScriptError` method allows the user to see a better `Type` or `Reference` Error Message. It will by default show for example:-
- Type of Error - ReferenceError
- Error Message - vals is not defined
- Function Name - fn1
- Line Number - 162:14

  The method takes in two optional arguments - 
   1. array of strings - pass in one or more of these:- (The order of display will depend on the order of passed in strings)
     - `type` - this will display the Type of Error message
     - `line` - this will display the Line Number
     - `message` - this will display the Error Message
     - `functionName` - this will display the Function Name
   2. offsetLineNumber - if needed give it a number to offset the `Line Number` if it is displayed incorrectly

## Usage:
```javascript

const fn1 = function(obj, keys) {
  let val = obj[keys[0]];
  return val
}   

expect(() => fn1({name: "Jack"}, ['name'])).to.not.throwJavaScriptError();
expect(() => fn1({name: "Jack"}, ['name'])).to.not.throwJavaScriptError(["functionName", "line"]);
```

# Chai `Check Method Use` Method

The `method` method allows the user test if a method is being used in a function.

## Usage:
```javascript

const fn1 = function(obj) {
  let values = Object.values()
}  
  
const fn2 = function(arr) {
  var result = [];
  for(var i = 0; i < arr.length; i++) {
    result.push(arr[i]);
  }
  return result;
};

// pass the method name as a string
expect(fn1).to.have.method('values');

// pass the method name as a string
expect(fn2).to.not.have.method('slice');

```

# Chai `Ordered Console logs` Method

The `callOrderedConsoleLogsWith` method allows the user to test if log/s are displayed in required Order.

## Usage:
  
  The method takes in two arguments -
   - array of arrays or just an array of expected logs- expected logs to be logged by student;
   - (optional) array of arguments as will be passed to the tested function. if no argument - then ignore.
  
  Note:- The method ignores extra logs by student and only checks the expected logs.
```javascript

const fn1 = function() {
  for(var i = 0; i < 4; i++) {
    console.log("Jack", JSON.stringify({a: 1}))
  }
};

expect(fn1).to.callOrderedConsoleLogsWith([[ "Jack" , {a: 1}],
                                          [ "Jack" , {b: 1}],
                                          [ "Jack" , {a: 1}],
                                          [ "Jack" , {a: 1}]]);

expect(fn1).to.callOrderedConsoleLogsWith([[ "Jack" , {a: 1}, "Jack" , {b: 1}, "Jack" , {b: 1}, "Jack" , {b: 1}]]);

// When function takes in arguments.
let obj = {a:1, b:2};
let key = ['a', 'b']
const fn2 = function(o, k) {
  console.log(o[k[0]]);
  console.log(o[k[1]])
};

expect(fn2).to.callUnorderedConsoleLogsWith([[1, 2]], [obj, key]);
```


# Chai `Unordered Console logs` Method

The `callUnorderedConsoleLogsWith` method allows the user to test if log/s are displayed when order of logs don't matter. 

  The method takes in two arguments -
   - array of arrays - expected logs to be logged by student;
   - (optional) array of arguments as will be passed to the tested function. if no argument - then not need to pass anything.

  Note:- The method ignores extra logs by student and only checks the expected logs
## Usage:

```javascript

const fn1 = function() {
  console.log("jack", "jill");
  console.log(JSON.stringify({a:1}));
};

expect(fn1).to.callUnorderedConsoleLogsWith([
  [{a: 1}],
  ['jack', 'jill']
]);

expect(fn1).to.callUnorderedConsoleLogsWith([
  [{a: 1}, 'jack', 'jill']
]);

// When function takes in arguments.
let obj = {a:1};
let key = ['a']
const fn2 = function(o, k) {
  console.log(o[k[0]]);
};

expect(fn2).to.callUnorderedConsoleLogsWith([[1]], [obj, key]);

```

---
_Source: https://npm.io/package/chai-extra-functionblock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
