Skip to content

Commit

Permalink
test(mocks): explicit jest spy instance
Browse files Browse the repository at this point in the history
  • Loading branch information
sabertazimi committed Aug 10, 2021
1 parent f19f668 commit d5e0cfd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 36 deletions.
24 changes: 14 additions & 10 deletions src/hooks/useBuildTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import useBuildTime from './useBuildTime';
const buildTime = new Date(2021, 0, 1, 8, 0, 0).toLocaleString('zh-CN');

describe('useBuildTime', () => {
beforeAll(() => {
jest.spyOn(gatsby, 'useStaticQuery').mockImplementation(() => {
return {
site: {
buildTime,
},
};
});
let mockUseStaticQuery: jest.SpyInstance;

beforeEach(() => {
mockUseStaticQuery = jest
.spyOn(gatsby, 'useStaticQuery')
.mockImplementation(() => {
return {
site: {
buildTime,
},
};
});
});

afterAll(() => {
(gatsby.useStaticQuery as unknown as jest.SpyInstance).mockRestore();
afterEach(() => {
mockUseStaticQuery.mockRestore();
});

test('should return date time', () => {
Expand Down
36 changes: 20 additions & 16 deletions src/hooks/usePostsMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ describe('usePostsMetadata', () => {
test.each(testIterator)(
`should return correct [%i / ${testCount}] post metadata`,
(index) => {
jest.spyOn(gatsby, 'useStaticQuery').mockImplementation(() => {
return {
allMarkdownRemark: {
edges: Posts,
},
};
});
const mockUseStaticQuery = jest
.spyOn(gatsby, 'useStaticQuery')
.mockImplementation(() => {
return {
allMarkdownRemark: {
edges: Posts,
},
};
});

const { posts, tags } = usePostsMetadata();

Expand Down Expand Up @@ -73,20 +75,22 @@ describe('usePostsMetadata', () => {
expect(tags['Frontend Development']).toBe(5);
expect(tags['Web Development']).toBe(5);

(gatsby.useStaticQuery as unknown as jest.SpyInstance).mockRestore();
mockUseStaticQuery.mockRestore();
}
);

test.each(testIterator)(
`should return correct [%i / ${testCount}] post metadata with partial data`,
(index) => {
jest.spyOn(gatsby, 'useStaticQuery').mockImplementation(() => {
return {
allMarkdownRemark: {
edges: BasePosts,
},
};
});
const mockUseStaticQuery = jest
.spyOn(gatsby, 'useStaticQuery')
.mockImplementation(() => {
return {
allMarkdownRemark: {
edges: BasePosts,
},
};
});

const { posts, tags } = usePostsMetadata();

Expand Down Expand Up @@ -118,7 +122,7 @@ describe('usePostsMetadata', () => {
// Check correct empty tags data of post are returned
expect(tags).toMatchObject({});

(gatsby.useStaticQuery as unknown as jest.SpyInstance).mockRestore();
mockUseStaticQuery.mockRestore();
}
);
});
28 changes: 18 additions & 10 deletions src/hooks/useSiteMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,25 @@ const siteMetadata = {
};

describe('useSiteMetadata', () => {
test(`should return correct site metadata`, () => {
jest.spyOn(gatsby, 'useStaticQuery').mockImplementation(() => {
return {
site: {
siteMetadata,
},
};
});
let mockUseStaticQuery: jest.SpyInstance;

beforeEach(() => {
mockUseStaticQuery = jest
.spyOn(gatsby, 'useStaticQuery')
.mockImplementation(() => {
return {
site: {
siteMetadata,
},
};
});
});

afterEach(() => {
mockUseStaticQuery.mockRestore();
});

test(`should return correct site metadata`, () => {
const {
title,
author,
Expand Down Expand Up @@ -66,7 +76,5 @@ describe('useSiteMetadata', () => {
siteMetadata['bookList'][index]['description']
);
});

(gatsby.useStaticQuery as unknown as jest.SpyInstance).mockRestore();
});
});

0 comments on commit d5e0cfd

Please sign in to comment.