@khanacademy/wonder-blocks-data 2.3.3 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/es/index.js +365 -429
  3. package/dist/index.js +455 -461
  4. package/docs.md +19 -13
  5. package/package.json +6 -6
  6. package/src/__tests__/__snapshots__/generated-snapshot.test.js.snap +40 -160
  7. package/src/__tests__/generated-snapshot.test.js +15 -195
  8. package/src/components/__tests__/data.test.js +159 -965
  9. package/src/components/__tests__/gql-router.test.js +64 -0
  10. package/src/components/__tests__/intercept-data.test.js +9 -66
  11. package/src/components/__tests__/track-data.test.js +6 -5
  12. package/src/components/data.js +9 -119
  13. package/src/components/data.md +38 -60
  14. package/src/components/gql-router.js +66 -0
  15. package/src/components/intercept-context.js +2 -3
  16. package/src/components/intercept-data.js +2 -34
  17. package/src/components/intercept-data.md +7 -105
  18. package/src/hooks/__tests__/use-data.test.js +826 -0
  19. package/src/hooks/__tests__/use-gql.test.js +233 -0
  20. package/src/hooks/use-data.js +143 -0
  21. package/src/hooks/use-gql.js +75 -0
  22. package/src/index.js +7 -9
  23. package/src/util/__tests__/get-gql-data-from-response.test.js +187 -0
  24. package/src/util/__tests__/memory-cache.test.js +134 -35
  25. package/src/util/__tests__/request-fulfillment.test.js +21 -36
  26. package/src/util/__tests__/request-handler.test.js +30 -30
  27. package/src/util/__tests__/request-tracking.test.js +29 -30
  28. package/src/util/__tests__/response-cache.test.js +521 -561
  29. package/src/util/__tests__/result-from-cache-entry.test.js +68 -0
  30. package/src/util/get-gql-data-from-response.js +69 -0
  31. package/src/util/gql-error.js +36 -0
  32. package/src/util/gql-router-context.js +6 -0
  33. package/src/util/gql-types.js +60 -0
  34. package/src/util/memory-cache.js +20 -15
  35. package/src/util/request-fulfillment.js +4 -0
  36. package/src/util/request-handler.js +4 -28
  37. package/src/util/request-handler.md +0 -32
  38. package/src/util/request-tracking.js +2 -3
  39. package/src/util/response-cache.js +50 -110
  40. package/src/util/result-from-cache-entry.js +38 -0
  41. package/src/util/types.js +14 -35
  42. package/LICENSE +0 -21
  43. package/src/components/__tests__/intercept-cache.test.js +0 -124
  44. package/src/components/__tests__/internal-data.test.js +0 -1030
  45. package/src/components/intercept-cache.js +0 -79
  46. package/src/components/intercept-cache.md +0 -103
  47. package/src/components/internal-data.js +0 -219
  48. package/src/util/__tests__/no-cache.test.js +0 -112
  49. package/src/util/no-cache.js +0 -66
  50. package/src/util/no-cache.md +0 -66
@@ -1,17 +1,14 @@
1
- Sometimes, you may want to generate tests that check the loading state and
2
- subsequent loaded state are working correctly for your uses of `Data`.
3
- So, rather than manipulating cache usage with `InterceptCache` you can
4
- manipulate request fulfillment and cache refresh using the `InterceptData`
5
- component.
1
+ When you want to generate tests that check the loading state and
2
+ subsequent loaded state are working correctly for your uses of `Data` you can
3
+ use the `InterceptData` component.
6
4
 
7
- This component takes five props; children to be rendered, the handler of the
8
- type of data requests that are to be intercepted, and either a `fulfillRequest`
9
- method, a `shouldRefreshCache` method, or both.
5
+ This component takes four props; children to be rendered, the handler of the
6
+ type of data requests that are to be intercepted, and a `fulfillRequest`.
10
7
 
11
8
  Note that this component is expected to be used only within test cases and
12
9
  usually only as a single instance. In flight requests for a given handler
13
10
  type can be shared and as such, using `InterceptData` alongside non-intercepted
14
- `Data` components with the same handler type can have undetermined outcomes.
11
+ `Data` components with the same handler type can have indeterminate outcomes.
15
12
 
16
13
  The `fulfillRequest` intercept function has the form:
17
14
 
@@ -19,7 +16,7 @@ The `fulfillRequest` intercept function has the form:
19
16
  (options: TOptions) => ?Promise<TData>;
20
17
  ```
21
18
 
22
- If this method is omitted or returns `null`, the default behavior occurs. This
19
+ If this method returns `null`, the default behavior occurs. This
23
20
  means that a request will be made for data via the handler assigned to the
24
21
  `Data` component being intercepted.
25
22
 
@@ -66,98 +63,3 @@ const fulfillRequestInterceptor = function(options) {
66
63
  </View>
67
64
  </InterceptData>
68
65
  ```
69
-
70
- The `shouldRefreshCache` intercept function has the form:
71
-
72
- ```js static
73
- (options: TOptions, cachedEntry: ?$ReadOnly<CacheEntry<TData>>) => ?boolean;
74
- ```
75
-
76
- If this method is omitted or returns `null`, the default behavior occurs. This
77
- means that if `cacheEntry` has a value, that will be used; otherwise, the
78
- `shouldRefreshCache` method of the handler assigned to the `Data` component
79
- being intercepted will be invoked.
80
-
81
- ```jsx
82
- import {Body, BodyMonospace} from "@khanacademy/wonder-blocks-typography";
83
- import {View} from "@khanacademy/wonder-blocks-core";
84
- import {withActionScheduler} from "@khanacademy/wonder-blocks-timing";
85
- import {InterceptData, Data, RequestHandler} from "@khanacademy/wonder-blocks-data";
86
- import {Strut} from "@khanacademy/wonder-blocks-layout";
87
- import Color from "@khanacademy/wonder-blocks-color";
88
- import Spacing from "@khanacademy/wonder-blocks-spacing";
89
-
90
- class MyHandler extends RequestHandler {
91
- constructor() {
92
- super("INTERCEPT_DATA_HANDLER2");
93
- let _counter = 0;
94
- this.fulfillRequest = function(options) {
95
- return Promise.resolve(`DATA ${_counter++}`);
96
- }
97
- }
98
-
99
- shouldRefreshData(options) {
100
- return false;
101
- }
102
- }
103
-
104
- const handler = new MyHandler();
105
- const shouldRefreshCacheInterceptor = function(options) {
106
- if (options === "DATA") {
107
- return true;
108
- }
109
- return null;
110
- };
111
-
112
- class SchedulableExample extends React.Component {
113
- constructor(props) {
114
- super(props);
115
- this.state = {
116
- stamp: Date.now(),
117
- };
118
- }
119
-
120
- componentDidMount() {
121
- this.props.schedule.interval(() => this.setState({
122
- stamp: Date.now(),
123
- }), 1000);
124
- }
125
-
126
- render() {
127
- /**
128
- * The key on the View is what causes the re-render.
129
- *
130
- * The re-render causes the `Data` component to render again.
131
- * That causes it to check if it should refresh the cache.
132
- * and that in turn causes a new data request that updates the value
133
- * being rendered.
134
- *
135
- * Without the key to cause the re-render, no additional request would
136
- * be made.
137
- */
138
- return (
139
- <InterceptData handler={handler} shouldRefreshCache={shouldRefreshCacheInterceptor}>
140
- <View key={this.state.stamp}>
141
- <Body>This re-renders once a second. On the render, the cache is refreshed and so we see an update.</Body>
142
- <Data handler={handler} options={"DATA"}>
143
- {({loading, data}) => {
144
- if (loading) {
145
- return "If you see this, the example is broken!";
146
- }
147
-
148
- return (
149
- <BodyMonospace>{data}</BodyMonospace>
150
- );
151
- }}
152
- </Data>
153
- </View>
154
- </InterceptData>
155
- );
156
- }
157
- }
158
-
159
- const Example = withActionScheduler(SchedulableExample);
160
-
161
- <Example />
162
-
163
- ```