Skip to content

Commit

Permalink
Re-factor test cases to use jest.spyOn intead of Jasmine spyOn
Browse files Browse the repository at this point in the history
  • Loading branch information
micksatana committed Nov 19, 2018
1 parent 81bbb09 commit 9b4ece6
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 142 deletions.
62 changes: 32 additions & 30 deletions test/functions-config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ describe('FunctionsConfig', () => {
});

it('should execute with correct command', () => {
expect(ChildProcess.exec).toBeCalledWith(`firebase functions:config:get`, expect.anything());
expect(ChildProcess.exec).toHaveBeenCalledWith(`firebase functions:config:get`, expect.anything());
});

it('should return Firebase Functions config', () => {
expect(expectedConfig).toEqual(fakeConfig);
});

afterAll(() => {
ChildProcess.exec.restore();
ChildProcess.exec.mockRestore();
});

});
Expand All @@ -48,15 +48,15 @@ describe('FunctionsConfig', () => {
});

it('should execute with correct command', () => {
expect(ChildProcess.exec).toBeCalledWith(`firebase functions:config:get ${fakeParam}`, expect.anything());
expect(ChildProcess.exec).toHaveBeenCalledWith(`firebase functions:config:get ${fakeParam}`, expect.anything());
});

it('should return Firebase Functions config', () => {
expect(expectedConfig).toEqual(fakeConfig[fakeParam]);
});

afterAll(() => {
ChildProcess.exec.restore();
ChildProcess.exec.mockRestore();
});

});
Expand All @@ -73,7 +73,7 @@ describe('FunctionsConfig', () => {
});

afterAll(() => {
ChildProcess.exec.restore();
ChildProcess.exec.mockRestore();
});

});
Expand All @@ -83,16 +83,18 @@ describe('FunctionsConfig', () => {

beforeAll(() => {
ChildProcess.exec.mockImplementation((command, callback) => callback(null, JSON.stringify(fakeConfig)));
spyOn(JSON, 'parse').and.throwError(fakeError);
jest.spyOn(JSON, 'parse').mockImplementation(() => {
throw fakeError;
});
});

it('should reject with the error', () => {
expect(FunctionsConfig.get()).rejects.toEqual(fakeError);
});

afterAll(() => {
ChildProcess.exec.restore();
JSON.parse.restore();
ChildProcess.exec.mockRestore();
JSON.parse.mockRestore();
});

});
Expand All @@ -113,15 +115,15 @@ describe('FunctionsConfig', () => {
});

it('should execute with correct command', () => {
expect(ChildProcess.exec).toBeCalledWith(`firebase functions:config:set ${name}=${value}`, expect.anything());
expect(ChildProcess.exec).toHaveBeenCalledWith(`firebase functions:config:set ${name}=${value}`, expect.anything());
});

it('should return value', () => {
expect(expectedValue).toEqual(value);
});

afterAll(() => {
ChildProcess.exec.restore();
ChildProcess.exec.mockRestore();
});

});
Expand All @@ -138,7 +140,7 @@ describe('FunctionsConfig', () => {
});

afterAll(() => {
ChildProcess.exec.restore();
ChildProcess.exec.mockRestore();
});

});
Expand All @@ -158,15 +160,15 @@ describe('FunctionsConfig', () => {
});

it('should execute with correct command', () => {
expect(ChildProcess.exec).toBeCalledWith(`firebase functions:config:unset ${name}`, expect.anything());
expect(ChildProcess.exec).toHaveBeenCalledWith(`firebase functions:config:unset ${name}`, expect.anything());
});

it('should return value', () => {
expect(expectedResult).toEqual(name);
});

afterAll(() => {
ChildProcess.exec.restore();
ChildProcess.exec.mockRestore();
});

});
Expand All @@ -183,7 +185,7 @@ describe('FunctionsConfig', () => {
});

afterAll(() => {
ChildProcess.exec.restore();
ChildProcess.exec.mockRestore();
});

});
Expand All @@ -204,7 +206,7 @@ describe('FunctionsConfig', () => {
let result;

beforeAll(async () => {
spyOn(FunctionsConfig, 'get').and.returnValue(Promise.resolve(config));
jest.spyOn(FunctionsConfig, 'get').mockResolvedValue(config);
result = await FunctionsConfig.getNamesById(group, id);
});

Expand All @@ -217,7 +219,7 @@ describe('FunctionsConfig', () => {
});

afterAll(() => {
FunctionsConfig.get.restore();
FunctionsConfig.get.mockRestore();
});

});
Expand All @@ -226,7 +228,7 @@ describe('FunctionsConfig', () => {
let result;

beforeAll(async () => {
spyOn(FunctionsConfig, 'get');
jest.spyOn(FunctionsConfig, 'get');
result = await FunctionsConfig.getNamesById(group, id, config);
});

Expand All @@ -239,7 +241,7 @@ describe('FunctionsConfig', () => {
});

afterAll(() => {
FunctionsConfig.get.restore();
FunctionsConfig.get.mockRestore();
});

});
Expand All @@ -248,7 +250,7 @@ describe('FunctionsConfig', () => {
let result;

beforeAll(async () => {
spyOn(FunctionsConfig, 'get');
jest.spyOn(FunctionsConfig, 'get');
result = await FunctionsConfig.getNamesById(group, 'some-unknown-id', config);
});

Expand All @@ -261,7 +263,7 @@ describe('FunctionsConfig', () => {
});

afterAll(() => {
FunctionsConfig.get.restore();
FunctionsConfig.get.mockRestore();
});

});
Expand All @@ -270,7 +272,7 @@ describe('FunctionsConfig', () => {
let result;

beforeAll(async () => {
spyOn(FunctionsConfig, 'get').and.returnValue(Promise.resolve(config));
jest.spyOn(FunctionsConfig, 'get').mockResolvedValue(config);
result = await FunctionsConfig.getNamesById('unknown-group', 'some-unknown-id', config);
});

Expand All @@ -283,7 +285,7 @@ describe('FunctionsConfig', () => {
});

afterAll(() => {
FunctionsConfig.get.restore();
FunctionsConfig.get.mockRestore();
});

});
Expand All @@ -304,7 +306,7 @@ describe('FunctionsConfig', () => {
let result;

beforeAll(async () => {
spyOn(FunctionsConfig, 'get').and.returnValue(Promise.resolve(config));
jest.spyOn(FunctionsConfig, 'get').mockResolvedValue(config);
result = await FunctionsConfig.getIdByName(group, name);
});

Expand All @@ -317,7 +319,7 @@ describe('FunctionsConfig', () => {
});

afterAll(() => {
FunctionsConfig.get.restore();
FunctionsConfig.get.mockRestore();
});

});
Expand All @@ -326,7 +328,7 @@ describe('FunctionsConfig', () => {
let result;

beforeAll(async () => {
spyOn(FunctionsConfig, 'get');
jest.spyOn(FunctionsConfig, 'get');
result = await FunctionsConfig.getIdByName(group, name, config);
});

Expand All @@ -339,7 +341,7 @@ describe('FunctionsConfig', () => {
});

afterAll(() => {
FunctionsConfig.get.restore();
FunctionsConfig.get.mockRestore();
});

});
Expand All @@ -348,7 +350,7 @@ describe('FunctionsConfig', () => {
let result;

beforeAll(async () => {
spyOn(FunctionsConfig, 'get');
jest.spyOn(FunctionsConfig, 'get');
result = await FunctionsConfig.getIdByName(group, 'some-unknown-name', config);
});

Expand All @@ -361,7 +363,7 @@ describe('FunctionsConfig', () => {
});

afterAll(() => {
FunctionsConfig.get.restore();
FunctionsConfig.get.mockRestore();
});

});
Expand All @@ -370,7 +372,7 @@ describe('FunctionsConfig', () => {
let result;

beforeAll(async () => {
spyOn(FunctionsConfig, 'get').and.returnValue(Promise.resolve(config));
jest.spyOn(FunctionsConfig, 'get').mockResolvedValue(config);
result = await FunctionsConfig.getIdByName('unknown-group', name, config);
});

Expand All @@ -383,7 +385,7 @@ describe('FunctionsConfig', () => {
});

afterAll(() => {
FunctionsConfig.get.restore();
FunctionsConfig.get.mockRestore();
});

});
Expand Down
12 changes: 6 additions & 6 deletions test/liff-add-request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('LIFFAddRequest', () => {
let accessToken = 'someaccesstoken';

beforeAll(() => {
spyOn(Axios, 'create').and.callThrough();
jest.spyOn(Axios, 'create');
req = new LIFFAddRequest({ accessToken });
});

Expand All @@ -17,7 +17,7 @@ describe('LIFFAddRequest', () => {
});

it('should create axios instance with correct headers for LINE API', () => {
expect(Axios.create).toBeCalledWith({
expect(Axios.create).toHaveBeenCalledWith({
headers: {
'authorization': `Bearer ${accessToken}`,
'content-type': 'application/json'
Expand All @@ -29,20 +29,20 @@ describe('LIFFAddRequest', () => {
describe('when send data', () => {
let data = { some: 'data' };
beforeAll(() => {
spyOn(req.axios, 'post').and.returnValue(Promise.resolve('any'));
jest.spyOn(req.axios, 'post').mockResolvedValue('any');
req.send(data);
});
it('should call to correct endpoint', () => {
expect(req.axios.post).toHaveBeenCalledTimes(1);
expect(req.axios.post).toBeCalledWith(req.endpoint, JSON.stringify(data));
expect(req.axios.post).toHaveBeenCalledWith(req.endpoint, JSON.stringify(data));
});
afterAll(() => {
req.axios.post.restore();
req.axios.post.mockRestore();
});
});

afterAll(() => {
Axios.create.restore();
Axios.create.mockRestore();
});

});
Expand Down
Loading

0 comments on commit 9b4ece6

Please sign in to comment.