Skip to content

Commit

Permalink
dont inject, stub the dependency using sinon
Browse files Browse the repository at this point in the history
  • Loading branch information
soryy708 committed Apr 24, 2024
1 parent a6b26c4 commit f64a123
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
11 changes: 1 addition & 10 deletions src/scc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,8 @@ import ExportMapBuilder from './exportMap/builder';
import childContext from './exportMap/childContext';

let cache = new Map();
let exportMapBuilder = ExportMapBuilder;

export default class StronglyConnectedComponentsBuilder {
static injectExportMapBuilder(value) {
exportMapBuilder = value;
}

static resetExportMapBuilder() {
exportMapBuilder = ExportMapBuilder;
}

static clearCache() {
cache = new Map();
}
Expand All @@ -37,7 +28,7 @@ export default class StronglyConnectedComponentsBuilder {
}

static calculate(context) {
const exportMap = exportMapBuilder.for(context);
const exportMap = ExportMapBuilder.for(context);
const adjacencyList = this.exportMapToAdjacencyList(exportMap);
const calculatedScc = calculateScc(adjacencyList);
return StronglyConnectedComponentsBuilder.calculatedSccToPlainObject(calculatedScc);
Expand Down
23 changes: 11 additions & 12 deletions tests/src/scc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sinon from 'sinon';
import { expect } from 'chai';
import StronglyConnectedComponentsBuilder from '../../src/scc';
import ExportMapBuilder from '../../src/exportMap/builder';

function exportMapFixtureBuilder(path, imports) {
return {
Expand All @@ -10,9 +11,7 @@ function exportMapFixtureBuilder(path, imports) {
}

describe('Strongly Connected Components Builder', () => {
const fakeExportMapBuilder = { for: sinon.spy() };
before(() => StronglyConnectedComponentsBuilder.injectExportMapBuilder(fakeExportMapBuilder));
after(() => StronglyConnectedComponentsBuilder.resetExportMapBuilder());
afterEach(() => ExportMapBuilder.for.restore());
afterEach(() => StronglyConnectedComponentsBuilder.clearCache());

describe('When getting an SCC', () => {
Expand All @@ -26,7 +25,7 @@ describe('Strongly Connected Components Builder', () => {
describe('Given two files', () => {
describe('When they don\'t cycle', () => {
it('Should return foreign SCCs', () => {
fakeExportMapBuilder.for = sinon.stub().returns(
sinon.stub(ExportMapBuilder, 'for').returns(
exportMapFixtureBuilder('foo.js', [exportMapFixtureBuilder('bar.js', [])]),
);
const actual = StronglyConnectedComponentsBuilder.for(source, context);
Expand All @@ -36,7 +35,7 @@ describe('Strongly Connected Components Builder', () => {

describe('When they do cycle', () => {
it('Should return same SCC', () => {
fakeExportMapBuilder.for = sinon.stub().returns(
sinon.stub(ExportMapBuilder, 'for').returns(
exportMapFixtureBuilder('foo.js', [
exportMapFixtureBuilder('bar.js', [
exportMapFixtureBuilder('foo.js', []),
Expand All @@ -53,7 +52,7 @@ describe('Strongly Connected Components Builder', () => {
describe('When they form a line', () => {
describe('When A -> B -> C', () => {
it('Should return foreign SCCs', () => {
fakeExportMapBuilder.for = sinon.stub().returns(
sinon.stub(ExportMapBuilder, 'for').returns(
exportMapFixtureBuilder('foo.js', [
exportMapFixtureBuilder('bar.js', [
exportMapFixtureBuilder('buzz.js', []),
Expand All @@ -67,7 +66,7 @@ describe('Strongly Connected Components Builder', () => {

describe('When A -> B <-> C', () => {
it('Should return 2 SCCs, A on its own', () => {
fakeExportMapBuilder.for = sinon.stub().returns(
sinon.stub(ExportMapBuilder, 'for').returns(
exportMapFixtureBuilder('foo.js', [
exportMapFixtureBuilder('bar.js', [
exportMapFixtureBuilder('buzz.js', [
Expand All @@ -83,7 +82,7 @@ describe('Strongly Connected Components Builder', () => {

describe('When A <-> B -> C', () => {
it('Should return 2 SCCs, C on its own', () => {
fakeExportMapBuilder.for = sinon.stub().returns(
sinon.stub(ExportMapBuilder, 'for').returns(
exportMapFixtureBuilder('foo.js', [
exportMapFixtureBuilder('bar.js', [
exportMapFixtureBuilder('buzz.js', []),
Expand All @@ -98,7 +97,7 @@ describe('Strongly Connected Components Builder', () => {

describe('When A <-> B <-> C', () => {
it('Should return same SCC', () => {
fakeExportMapBuilder.for = sinon.stub().returns(
sinon.stub(ExportMapBuilder, 'for').returns(
exportMapFixtureBuilder('foo.js', [
exportMapFixtureBuilder('bar.js', [
exportMapFixtureBuilder('foo.js', []),
Expand All @@ -116,7 +115,7 @@ describe('Strongly Connected Components Builder', () => {

describe('When they form a loop', () => {
it('Should return same SCC', () => {
fakeExportMapBuilder.for = sinon.stub().returns(
sinon.stub(ExportMapBuilder, 'for').returns(
exportMapFixtureBuilder('foo.js', [
exportMapFixtureBuilder('bar.js', [
exportMapFixtureBuilder('buzz.js', [
Expand All @@ -132,7 +131,7 @@ describe('Strongly Connected Components Builder', () => {

describe('When they form a Y', () => {
it('Should return 3 distinct SCCs', () => {
fakeExportMapBuilder.for = sinon.stub().returns(
sinon.stub(ExportMapBuilder, 'for').returns(
exportMapFixtureBuilder('foo.js', [
exportMapFixtureBuilder('bar.js', []),
exportMapFixtureBuilder('buzz.js', []),
Expand All @@ -145,7 +144,7 @@ describe('Strongly Connected Components Builder', () => {

describe('When they form a Mercedes', () => {
it('Should return 1 SCC', () => {
fakeExportMapBuilder.for = sinon.stub().returns(
sinon.stub(ExportMapBuilder, 'for').returns(
exportMapFixtureBuilder('foo.js', [
exportMapFixtureBuilder('bar.js', [
exportMapFixtureBuilder('foo.js', []),
Expand Down

0 comments on commit f64a123

Please sign in to comment.