Skip to content

Commit

Permalink
test: adding unit tests for common (#113)
Browse files Browse the repository at this point in the history
* test: adding unit tests for common

* test: address code review comments

* test: resolving merge conflicts

* test: resolve merge conflicts
  • Loading branch information
anandsunderraman authored Feb 2, 2022
1 parent 6750ad8 commit 7564dad
Show file tree
Hide file tree
Showing 9 changed files with 5,054 additions and 13,597 deletions.
4 changes: 2 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
test/temp
test
output
__transpiled
__transpiled
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# .gitignore
coverage

# Node dependencies
node_modules/*
Expand Down
18,400 changes: 4,816 additions & 13,584 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"description": "Template for generating Go module based on async api",
"main": "index.js",
"scripts": {
"test": "echo \"no test specified yet\"",
"test": "npm run test:components",
"release": "semantic-release",
"lint": "eslint --max-warnings 0 --fix --config .eslintrc .",
"generate:readme:toc": "markdown-toc -i README.md",
"generate:assets": "npm run generate:readme:toc",
"bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION"
"bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION",
"test:components": "jest --coverage ./test/components"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -81,16 +82,20 @@
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-security": "^1.4.0",
"eslint-plugin-sonarjs": "^0.5.0",
"jest": "^27.4.7",
"markdown-toc": "^1.2.0",
"semantic-release": "^17.1.1"
},
"jest": {
"transform": {
"\\.m?jsx?$": "jest-esm-transformer"
},
"collectCoverageFrom": [
"{filters,hooks}/**/*.js",
"!hooks/generateExtraFormats.js"
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
}
3 changes: 1 addition & 2 deletions template/go.sum.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { File } from '@asyncapi/generator-react-sdk';

export default function({ asyncapi }) {

return (
<File name="go.sum">
{`
Expand Down Expand Up @@ -81,4 +80,4 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
`}
</File>
);
}
}
72 changes: 72 additions & 0 deletions test/components/common.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { GetProtocolFlags, GetSubscriberFlags, pascalCase } from '../../components/common';
import AsyncAPIDocument from '@asyncapi/parser/lib/models/asyncapi';
import parser from '@asyncapi/parser'
import fs from 'fs'
import path from 'path'

const docWithoutProtocols = fs.readFileSync(path.resolve(__dirname, '../files/docWithoutProtocols.yml'), 'utf8');
const docWithAMQPublisher = fs.readFileSync(path.resolve(__dirname, '../files/docWithAMQPublisher.yml'), 'utf8');
const docWithoutAMQPublisher = fs.readFileSync(path.resolve(__dirname, '../files/docWithoutAMQPublisher.yml'), 'utf8');

describe('GetProtocolFlags', () => {

it('should return protocols as false when no channels are present ', async function() {
const expected = {
hasAMQP: false
};

const doc = await parser.parse(docWithoutProtocols);
const result = GetProtocolFlags(doc);
expect(result).toEqual(expected);
})

it('should return amqp as true when channels with amqp bindings are present ', async function() {
const expected = {
hasAMQP: true
};

const doc = await parser.parse(docWithoutAMQPublisher);
const result = GetProtocolFlags(doc);
expect(result).toEqual(expected);
})

})

describe('GetSubscriberFlags', () => {
it('should return subscriberFlags as false when no channels are present ', async function() {
const expected = {
hasAMQPSub: false
};

const doc = await parser.parse(docWithoutProtocols);
const result = GetSubscriberFlags(doc);
expect(result).toEqual(expected);
})

it('should return subscriberFlags as false when publish channels are NOT present', async function() {
const expected = {
hasAMQPSub: false
};
const doc = await parser.parse(docWithoutAMQPublisher);
const result = GetSubscriberFlags(doc);
expect(result).toEqual(expected);
})

it('should return subscriberFlags as true when publish channels are present', async function() {
const expected = {
hasAMQPSub: true
};
const doc = await parser.parse(docWithAMQPublisher);
const result = GetSubscriberFlags(doc);
expect(result).toEqual(expected);
})
})

describe('pascalCase', () => {
it('should convert string to pascalCase ', () => {
const expected = "PS"

const result = pascalCase("pS");
expect(result).toEqual(expected);
})
})
60 changes: 60 additions & 0 deletions test/files/docWithAMQPublisher.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
asyncapi: '2.1.0'

info:
title: Streetlights API
version: '1.0.0'
description: |
The Smartylighting Streetlights API allows you
to remotely manage the city lights.
license:
name: Apache 2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0'

defaultContentType: application/json

servers:
local:
url: localhost:5672
protocol: amqp
security:
- user-password: []

channels:
light/measured:
bindings:
amqp:
is: routingKey
queue:
name: light/measured
durable: true
exclusive: true
autoDelete: false
vhost: /
bindingVersion: 0.2.0
publish:
summary: Inform about environmental lighting conditions for a particular streetlight.
operationId: onLightMeasured
message:
name: LightMeasured
payload:
$id: LightMeasured
additionalProperties: false
type: object
properties:
id:
type: integer
minimum: 0
description: Id of the streetlight.
lumens:
type: integer
minimum: 0
description: Light intensity measured in lumens.
sentAt:
type: string
format: date-time
description: Date and time when the message was sent.

components:
securitySchemes:
user-password:
type: userPassword
38 changes: 38 additions & 0 deletions test/files/docWithoutAMQPublisher.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
asyncapi: '2.1.0'

info:
title: Streetlights API
version: '1.0.0'
description: |
The Smartylighting Streetlights API allows you
to remotely manage the city lights.
license:
name: Apache 2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0'

defaultContentType: application/json

servers:
local:
url: localhost:5672
protocol: amqp
security:
- user-password: []

channels:
light/measured:
bindings:
amqp:
is: routingKey
queue:
name: light/measured
durable: true
exclusive: true
autoDelete: false
vhost: /
bindingVersion: 0.2.0

components:
securitySchemes:
user-password:
type: userPassword
50 changes: 50 additions & 0 deletions test/files/docWithoutProtocols.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
asyncapi: '2.1.0'

info:
title: Streetlights API
version: '1.0.0'
description: |
The Smartylighting Streetlights API allows you
to remotely manage the city lights.
license:
name: Apache 2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0'

defaultContentType: application/json

servers:
local:
url: localhost:5672
protocol: amqp
security:
- user-password: []

channels:
light/measured:
publish:
summary: Inform about environmental lighting conditions for a particular streetlight.
operationId: onLightMeasured
message:
name: LightMeasured
payload:
$id: LightMeasured
additionalProperties: false
type: object
properties:
id:
type: integer
minimum: 0
description: Id of the streetlight.
lumens:
type: integer
minimum: 0
description: Light intensity measured in lumens.
sentAt:
type: string
format: date-time
description: Date and time when the message was sent.

components:
securitySchemes:
user-password:
type: userPassword

0 comments on commit 7564dad

Please sign in to comment.