chore: add jest tests, cleanup rpc

This commit is contained in:
41666 2020-10-11 15:10:20 -04:00
parent 5977c35d38
commit ac830fc946
25 changed files with 2786 additions and 319 deletions

View file

@ -39,6 +39,7 @@ jobs:
"${GITHUB_WORKSPACE}/bin/bazel" query //src/... |\
grep +publish |\
xargs -l1 "${GITHUB_WORKSPACE}/bin/bazel" run \
-c opt \
--stamp \
--workspace_status_command hack/workspace_status.sh

View file

@ -5,6 +5,10 @@ load("@bazel_gazelle//:def.bzl", "gazelle")
gazelle(name = "gazelle")
exports_files(
["tsconfig.json"],
[
"tsconfig.json",
"jest.config.js",
"jest-reporter.js",
],
visibility = ["//visibility:public"],
)

56
hack/jest.bzl Normal file
View file

@ -0,0 +1,56 @@
# Copied from https://github.com/bazelbuild/rules_nodejs/blob/stable/examples/jest/jest.bzl
# Licensed under Apache-2.0, not modified.
load("@npm//jest-cli:index.bzl", "jest", _jest_test = "jest_test")
DEFAULT_DEPS = [
"@npm//ts-jest",
"@npm//enzyme",
"@npm//enzyme-adapter-react-16",
"@npm//@types/enzyme",
"@npm//jest-environment-enzyme",
"@npm//jsdom",
"@npm//jest",
"@npm//jest-enzyme",
"@npm//jest-styled-components",
"@npm//enzyme-to-json",
"@npm//react-dom",
"@npm//@types/react-dom",
"//:tsconfig.json",
]
def _impl_jest_test(name, srcs, deps, jest_config, **kwargs):
"A macro around the autogenerated jest_test rule"
templated_args = [
"--no-cache",
"--no-watchman",
"--ci",
"--colors",
]
templated_args.extend(["--config", "$(rootpath %s)" % jest_config])
for src in srcs:
templated_args.extend(["--runTestsByPath", "$(rootpath %s)" % src])
data = [jest_config] + srcs + deps + ["//:jest-reporter.js"]
_jest_test(
name = name,
data = data,
templated_args = templated_args,
**kwargs
)
# This rule is used specifically to update snapshots via `bazel run`
jest(
name = "%s.update" % name,
data = data,
templated_args = templated_args + ["-u"],
**kwargs
)
def jest_test(src, deps = []):
_impl_jest_test(
name = src[1:] + "_test",
srcs = native.glob(["*.spec.ts", "*.spec.tsx"]),
deps = [src] + deps + DEFAULT_DEPS,
jest_config = "//:jest.config.js",
)

View file

@ -3,9 +3,17 @@ load("@npm//@bazel/typescript:index.bzl", "ts_library")
def _render_deps(deps = []):
output_deps = []
has_added_grpc_deps = False
for dep in deps:
if dep.startswith("//src/rpc"):
output_deps.append(dep + ":ts")
if has_added_grpc_deps == False:
output_deps.extend([
"@npm//google-protobuf",
"@npm//@improbable-eng/grpc-web",
])
has_added_grpc_deps = True
elif dep.startswith("//"):
output_deps.append(dep)
else:

13
jest-reporter.js Normal file
View file

@ -0,0 +1,13 @@
class BazelReporter {
onRunComplete(_, results) {
if (results.numFailedTests && results.snapshot.failure) {
console.log(`================================================================================
Snapshot failed, you can update the snapshot by running
bazel run ${process.env['TEST_TARGET'].replace(/_bin$/, '')}.update
`);
}
}
}
module.exports = BazelReporter;

13
jest.config.js Normal file
View file

@ -0,0 +1,13 @@
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig.json');
module.exports = {
preset: 'ts-jest',
testEnvironment: 'enzyme',
reporters: ['default', './jest-reporter'],
setupFilesAfterEnv: ['jest-enzyme', 'jest-styled-components'],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>/',
}),
snapshotSerializers: ['enzyme-to-json/serializer'],
};

View file

@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "https://roleypoly.com",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "jest",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook",
"now-build": "build-storybook",
@ -41,12 +41,26 @@
"@storybook/react": "^6.0.26",
"@storybook/theming": "^6.0.26",
"@types/chroma-js": "2.1.0",
"@types/enzyme": "^3.10.7",
"@types/enzyme-adapter-react-16": "^1.0.6",
"@types/google-protobuf": "3.7.3",
"@types/jest": "^26.0.14",
"@types/react": "^16.9.8",
"@types/react-dom": "^16.9.8",
"@types/styled-components": "5.1.4",
"babel-jest": "^26.5.2",
"babel-loader": "^8.1.0",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.5",
"enzyme-to-json": "^3.6.1",
"jest": "^26.5.3",
"jest-cli": "^26.5.2",
"jest-environment-enzyme": "^7.1.2",
"jest-enzyme": "^7.1.2",
"jest-styled-components": "^7.0.3",
"prettier": "^2.1.2",
"protoc-gen-ts": "^0.3.4",
"react-is": "^16.13.1",
"ts-jest": "^26.4.1",
"ts-protoc-gen": "^0.13.0",
"tsconfig-paths-webpack-plugin": "^3.3.0",
"tslint": "6.1.3",
@ -55,4 +69,4 @@
"tslint-plugin-prettier": "^2.3.0",
"typescript": "^4.0.3"
}
}
}

View file

@ -1,4 +1,5 @@
load("//:hack/react.bzl", "react_library")
load("//:hack/jest.bzl", "jest_test")
package(default_visibility = ["//visibility:public"])
@ -14,3 +15,7 @@ react_library(
"@types/styled-components",
],
)
jest_test(
src = ":button",
)

View file

@ -1,4 +1,5 @@
load("//:hack/react.bzl", "react_library")
load("//:hack/jest.bzl", "jest_test")
package(default_visibility = ["//visibility:public"])
@ -17,3 +18,8 @@ react_library(
"@types/styled-components",
],
)
jest_test(
src = ":role",
deps = ["//hack/fixtures"],
)

View file

@ -1,5 +1,5 @@
import { shallow } from 'enzyme';
import { roleCategory } from 'hack/fixtures/storyData';
import { roleCategory } from 'roleypoly/hack/fixtures/storyData';
import * as React from 'react';
import { Role } from './Role';

View file

@ -6,10 +6,8 @@ import { TabTitle, TabContent } from './TabView.styled';
const makeView = (props: Partial<TabViewProps> = {}) =>
shallow(
<TabView {...props}>
{{
'Tab 1': <Tab>{() => <div>tab 1</div>}</Tab>,
'Tab 2': <Tab>{() => <div>tab 2</div>}</Tab>,
}}
<Tab title="Tab 1">{() => <div>tab 1</div>}</Tab>
<Tab title="Tab 2">{() => <div>tab 2</div>}</Tab>,
</TabView>
);
@ -20,13 +18,13 @@ it('renders tab content correctly', () => {
});
it('automatically picks preselected tab content', () => {
const view = makeView({ initialTab: 'Tab 2' });
const view = makeView({ initialTab: 1 });
expect(view.find(Tab).renderProp('children')().text()).toBe('tab 2');
});
it('automatically uses the first tab when preselected tab is not present', () => {
const view = makeView({ initialTab: 'Not a Tab' });
const view = makeView({ initialTab: -1 });
view.find(TabContent).find('i').simulate('load');
expect(view.find(Tab).renderProp('children')().text()).toBe('tab 1');

View file

@ -1,4 +1,5 @@
load("//:hack/react.bzl", "react_library")
load("//:hack/jest.bzl", "jest_test")
package(default_visibility = ["//visibility:public"])
@ -9,3 +10,7 @@ react_library(
"@types/react",
],
)
jest_test(
src = ":timings",
)

View file

@ -1,28 +0,0 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("//:hack/tsproto.bzl", "ts_proto")
proto_library(
name = "auth_proto",
srcs = ["shared.proto"],
visibility = ["//visibility:public"],
)
go_proto_library(
name = "auth_go_proto",
importpath = "github.com/roleypoly/roleypoly/src/rpc/auth",
proto = ":auth_proto",
visibility = ["//visibility:public"],
)
go_library(
name = "auth",
embed = [":auth_go_proto"],
importpath = "github.com/roleypoly/roleypoly/src/rpc/auth",
visibility = ["//visibility:public"],
)
ts_proto(
proto = ":auth_proto",
)

View file

@ -1,37 +0,0 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("//:hack/tsproto.bzl", "ts_proto")
proto_library(
name = "backend_proto",
srcs = ["auth-backend.proto"],
visibility = ["//visibility:public"],
deps = [
"//src/rpc/auth:auth_proto",
"//src/rpc/shared:shared_proto",
],
)
go_proto_library(
name = "backend_go_proto",
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
importpath = "github.com/roleypoly/roleypoly/src/rpc/auth/backend",
proto = ":backend_proto",
visibility = ["//visibility:public"],
deps = [
"//src/rpc/auth",
"//src/rpc/shared",
],
)
go_library(
name = "backend",
embed = [":backend_go_proto"],
importpath = "github.com/roleypoly/roleypoly/src/rpc/auth/backend",
visibility = ["//visibility:public"],
)
ts_proto(
proto = ":backend_proto",
)

View file

@ -1,13 +0,0 @@
syntax = "proto3";
package roleypoly.auth.backend;
option go_package = "github.com/roleypoly/roleypoly/src/rpc/auth/backend";
import "src/rpc/shared/internal.proto";
import "src/rpc/shared/shared.proto";
import "src/rpc/auth/shared.proto";
service AuthBackend {
rpc GetNewAuthChallenge(roleypoly.IDQuery) returns (roleypoly.auth.AuthChallenge) {}
rpc GetSession(roleypoly.auth.Token) returns (roleypoly.RoleypolySession) {}
}

View file

@ -1,37 +0,0 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("//:hack/tsproto.bzl", "ts_proto")
proto_library(
name = "client_proto",
srcs = ["auth-client.proto"],
visibility = ["//visibility:public"],
deps = [
"//src/rpc/auth:auth_proto",
"//src/rpc/shared:shared_proto",
],
)
go_proto_library(
name = "client_go_proto",
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
importpath = "github.com/roleypoly/roleypoly/src/auth/client",
proto = ":client_proto",
visibility = ["//visibility:public"],
deps = [
"//src/rpc/auth",
"//src/rpc/shared",
],
)
go_library(
name = "client",
embed = [":client_go_proto"],
importpath = "github.com/roleypoly/roleypoly/src/auth/client",
visibility = ["//visibility:public"],
)
ts_proto(
proto = ":client_proto",
)

View file

@ -1,15 +0,0 @@
syntax = "proto3";
package roleypoly.auth.client;
option go_package = "github.com/roleypoly/roleypoly/src/auth/client";
import "src/rpc/shared/internal.proto";
import "src/rpc/shared/shared.proto";
import "src/rpc/auth/shared.proto";
service AuthClient {
rpc GetClientToken(roleypoly.Empty) returns (roleypoly.auth.Token) {}
rpc GetUserSession(roleypoly.Empty) returns (roleypoly.RoleypolySession) {}
rpc ResolveSessionKey(roleypoly.auth.Token) returns (roleypoly.auth.Token) {}
rpc AuthorizeChallenge(roleypoly.auth.AuthChallenge) returns (roleypoly.auth.Token) {}
}

View file

@ -1,22 +0,0 @@
syntax = "proto3";
package roleypoly.auth;
option go_package = "github.com/roleypoly/roleypoly/src/rpc/auth";
message Token {
string token = 1;
Type type = 2;
string state = 3;
enum Type {
unknown = 0;
sessionKey = 1;
clientToken = 2;
}
}
message AuthChallenge {
string userID = 1;
string magicUrl = 2;
string magicWords = 3;
}

View file

@ -1,31 +0,0 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("//:hack/tsproto.bzl", "ts_proto")
proto_library(
name = "ctf_proto",
srcs = ["ctf.proto"],
visibility = ["//visibility:public"],
deps = ["//src/rpc/shared:shared_proto"],
)
go_proto_library(
name = "ctf_go_proto",
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
importpath = "github.com/roleypoly/roleypoly/src/rpc/ctf",
proto = ":ctf_proto",
visibility = ["//visibility:public"],
deps = ["//src/rpc/shared"],
)
go_library(
name = "ctf",
embed = [":ctf_go_proto"],
importpath = "github.com/roleypoly/roleypoly/src/rpc/ctf",
visibility = ["//visibility:public"],
)
ts_proto(
proto = ":ctf_proto",
)

View file

@ -1,26 +0,0 @@
syntax = "proto3";
package roleypoly.ctf;
option go_package = "github.com/roleypoly/roleypoly/src/rpc/ctf";
import "src/rpc/shared/shared.proto";
service CTF {
rpc GetRingFlags (Ring) returns (Flags) {}
rpc CreateFlag (Flag) returns (Flag) {}
rpc PromoteFlag (Flag) returns (Flag) {}
rpc RemoveFlag (Flag) returns (roleypoly.Empty) {}
}
message Flags {
repeated Flag flags = 1;
}
message Flag {
string name = 1;
int32 ring = 2;
}
message Ring {
int32 ring = 1;
}

View file

@ -31,5 +31,6 @@ go_library(
)
ts_proto(
grpc = True,
proto = ":discord_proto",
)

View file

@ -33,5 +33,6 @@ go_library(
)
ts_proto(
grpc = True,
proto = ":platform_proto",
)

View file

@ -3,11 +3,6 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("//:hack/tsproto.bzl", "ts_proto")
package(default_visibility = [
"//hack/fixtures:__subpackages__",
"//src/design-system/atoms/role:__subpackages__",
])
proto_library(
name = "shared_proto",
srcs = [

View file

@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "esnext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"target": "es2019",
"module": "commonjs",
"lib": [
"dom",
"dom.iterable",
@ -19,10 +19,7 @@
"declaration": true,
"moduleResolution": "node",
"paths": {
"roleypoly/*": [
"*", // Enables absolute paths for src files in your project
"bazel-bin/*" // Enables referencing generate protos with absolute paths
]
"roleypoly/*": ["*", "bazel-bin/*"]
}
}
}

2735
yarn.lock

File diff suppressed because it is too large Load diff