Skip to content

Commit

Permalink
Feature/tests-for-data-fetcher (#266)
Browse files Browse the repository at this point in the history
* jest config

* Create utils.test.ts

* shared and utils test

* chore: Add Run-Tests job to deploy.yml

* Revert "chore: Add Run-Tests job to deploy.yml"

This reverts commit 5ac1860.

* create workflow for unit tests

* Update unit-tests.yml

* update test file matcher

---------

Co-authored-by: Dina Basumatary <[email protected]>
  • Loading branch information
dnafication and dnafication authored Oct 20, 2023
1 parent 849c253 commit 26ccecb
Show file tree
Hide file tree
Showing 8 changed files with 8,789 additions and 3,327 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Unit Tests

on: [push, pull_request]

jobs:
run-unit-tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Install Dependencies
run: npm install

- name: Run Unit Tests
run: npm test
206 changes: 206 additions & 0 deletions data/__tests__/shared.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import { Repository, Source } from "../../types";
import { getGitHubRepositories } from "../github";
import { getFilteredLanguages, getFilteredTags, processSource } from "../shared";
import { dummyRepositories } from "./test-data";

jest.mock("../github");

describe("shared", () => {
describe("processSource", () => {
it("returns an empty array when given an empty source", async () => {
const source: Source = {
name: "Empty Source",
provider: "github",
repositories: [],
labels: []
};
const result = await processSource(source);
expect(result).toEqual([]);
});

it("returns an array of repositories when given a source with repositories", async () => {
const source: Source = {
name: "React Repositories",
provider: "github",
repositories: ["facebook/react", "airbnb/react-dates"],
labels: []
};
(getGitHubRepositories as jest.Mock).mockResolvedValueOnce([
{
name: "React",
url: "https://github.com/facebook/react",
language: { id: "javascript", display: "JavaScript" },
tags: [{ id: "ui", display: "UI" }]
},
{
name: "React Dates",
url: "https://github.com/airbnb/react-dates",
language: { id: "javascript", display: "JavaScript" },
tags: [
{ id: "ui", display: "UI" },
{ id: "calendar", display: "Calendar" }
]
}
]);
const result = await processSource(source);
expect(result).toEqual([
{
name: "React",
url: "https://github.com/facebook/react",
language: { id: "javascript", display: "JavaScript" },
tags: [{ id: "ui", display: "UI" }]
},
{
name: "React Dates",
url: "https://github.com/airbnb/react-dates",
language: { id: "javascript", display: "JavaScript" },
tags: [
{ id: "ui", display: "UI" },
{ id: "calendar", display: "Calendar" }
]
}
]);
});
});

describe("getFilteredLanguages", () => {
it("returns an empty array when given an empty array of repositories", () => {
const result = getFilteredLanguages([]);
expect(result).toEqual([]);
});

it("returns an array of CountableLanguage objects when given an array of repositories", () => {
const result = getFilteredLanguages(dummyRepositories);
expect(result).toEqual([{ id: "python", display: "Python", count: 3 }]);
});

it("filters out languages with less than 3 repositories and sorts the remaining languages alphabetically", () => {
const repositories = [
{
name: "Angular1",
url: "https://github.com/angular/angular",
language: { id: "typescript", display: "TypeScript" },
tags: [{ id: "ui", display: "UI" }]
},
{
name: "Angular2",
url: "https://github.com/angular/angular",
language: { id: "typescript", display: "TypeScript" },
tags: [{ id: "ui", display: "UI" }]
},
{
name: "React",
url: "https://github.com/facebook/react",
language: { id: "javascript", display: "JavaScript" },
tags: [{ id: "ui", display: "UI" }]
},
{
name: "React Dates",
url: "https://github.com/airbnb/react-dates",
language: { id: "javascript", display: "JavaScript" },
tags: [
{ id: "ui", display: "UI" },
{ id: "calendar", display: "Calendar" }
]
},
{
name: "Vue.js",
url: "https://github.com/vuejs/vue",
language: { id: "javascript", display: "JavaScript" },
tags: [{ id: "ui", display: "UI" }]
},
{
name: "Angular",
url: "https://github.com/angular/angular",
language: { id: "typescript", display: "TypeScript" },
tags: [{ id: "ui", display: "UI" }]
}
] as Repository[];
const result = getFilteredLanguages(repositories);
console.log(result);
expect(result).toEqual([
{ id: "javascript", display: "JavaScript", count: 3 },
{ id: "typescript", display: "TypeScript", count: 3 }
]);
});
});

describe("getFilteredTags", () => {
it("returns an empty array when given an empty array of repositories", () => {
const result = getFilteredTags([]);
expect(result).toEqual([]);
});

it("returns an array of CountableTag objects when given an array of repositories", () => {
const repositories = [
{
name: "React",
url: "https://github.com/facebook/react",
language: { id: "javascript", display: "JavaScript" },
tags: [{ id: "ui", display: "UI" }]
},
{
name: "React Dates",
url: "https://github.com/airbnb/react-dates",
language: { id: "javascript", display: "JavaScript" },
tags: [
{ id: "ui", display: "UI" },
{ id: "calendar", display: "Calendar" }
]
},
{
name: "Vue.js",
url: "https://github.com/vuejs/vue",
language: { id: "javascript", display: "JavaScript" },
tags: [{ id: "ui", display: "UI" }]
}
] as Repository[];
const result = getFilteredTags(repositories);
expect(result).toEqual([{ id: "ui", display: "UI", count: 3 }]);
});

it("filters out tags with less than 3 repositories and sorts the remaining tags by count descending", () => {
const repositories = [
{
name: "React",
url: "https://github.com/facebook/react",
language: { id: "javascript", display: "JavaScript" },
tags: [{ id: "ui", display: "UI" }]
},
{
name: "React Dates",
url: "https://github.com/airbnb/react-dates",
language: { id: "javascript", display: "JavaScript" },
tags: [
{ id: "ui", display: "UI" },
{ id: "calendar", display: "Calendar" }
]
},
{
name: "Vue.js",
url: "https://github.com/vuejs/vue",
language: { id: "javascript", display: "JavaScript" },
tags: [
{ id: "ui", display: "UI" },
{ id: "calendar", display: "Calendar" }
]
},
{
name: "Angular",
url: "https://github.com/angular/angular",
language: { id: "typescript", display: "TypeScript" },
tags: [
{ id: "ui", display: "UI" },
{ id: "calendar", display: "Calendar" },
{ id: "angular", display: "Angular" }
]
}
] as Repository[];
const result = getFilteredTags(repositories);
expect(result).toEqual([
{ id: "ui", display: "UI", count: 4 },
{ id: "calendar", display: "Calendar", count: 3 }
]);
});
});
});
Loading

0 comments on commit 26ccecb

Please sign in to comment.